Category Archives: Powershell

Windows: Equivalent to linux time command

Hi,

the time command in Windows gets or sets the system time. If you need an equivalent to the linux time command – which measure the execution time of a given command – you can use the powershell commandlet Measure-Command. Limitation is that only the time can measure how long the command execution runs, not splited into user, system and realtime.
Continue reading Windows: Equivalent to linux time command

Advertisment to support michlstechblog.info

Powershell: List members of an Active Directory Group

Hi,

here are the code snippets to list all members of an Active Directory Group.

Some constants

# Define LDAP search root, the Global catalog of the domain
$sLDAPSearchRoot="LDAP://yourDomain.com:3268"
# The Groupname to looking for
$sGroupName="USR_GRP_IN_AD"

Continue reading Powershell: List members of an Active Directory Group

Powershell: A text based menu

Hi,

a long time ago I wrote a Menu function for quickly choose predefined options in a script. An example

Source the menu.ps1 in your script and call  fShowMenu. This will show the menu. The function needs 2 parameters:
Parameter 1: The menu title as string
Parameter 2: A hashtable with the Menuentries. The key of an Menuitem is returned by the function the corresponding value is shown at the menu.

# Source the file
. .\menu.ps1
# Call Menu funtion
fShowMenu "Choose your favorite Band" @{"sl"="Slayer";"me"="Metallica";"ex"="Exodus";"an"="Anthrax"}
A text based Powershell Menu
A text based Powershell Menu

Continue reading Powershell: A text based menu

Powershell: Error while calling the SaveAs Method of some Office Applications

Hi,

if you tried to save a word or excel document as another format you got an error. For example, if you want to convert a CSV File to a Excel workbook and save it in Excel 95/97/2003 format.

[reflection.assembly]::LoadWithPartialName("Microsoft.Office.InterOp.Excel")
$oExcel = New-Object -ComObject "Excel.Application"
$sCSVFile="D:\temp\ImportFile.csv"
$oExcel.Visible = $true
$oCSVWorkbook=$oExcel.Workbooks.Add()
$oExcel.Workbooks.OpenText($sCSVFile)
$oCSVWorkbook.SaveAs("D:\temp\test.xls",[Microsoft.Office.Interop.Excel.XlFileFormat]::xlExcel9795)
Exception calling "SaveAs" with "1" argument(s): "SaveAs method of Workbook class failed"
At D:\test\CSV2Excel.ps1:29 char:20
+ $xl_workbook.SaveAs <<<< ("D:\Temp\test.xls")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Continue reading Powershell: Error while calling the SaveAs Method of some Office Applications

Windows: Powershell script to fix and repair WMI

Hi,

this script repairs and recreates the WMI Repository of a Windows. For example, this occurs sometimes if you clone a Windows machine with an installed SCCM/SMS Client. The typical error in this case is “Failed to open to WMI namespace ‘\\.\root\CCM\SoftwareUpdates\DeploymentAgent’ (8007045b)”

Lasts start. Start a Powershell console as Administrator. Stop all services. Sometimes other services depends on the WMI Service and does not accept a stop request. Therefore you cannot stop the WMI service. TrendMicro Office is such a service. You have to kill them by TaskManager.

# Stop WMI
# Only if SCCM/SMS Client is installed. Stop ccmexec.
Stop-Service -Force ccmexec -ErrorAction SilentlyContinue
Stop-Service -Force winmgmt

(Re)Register WMI binary components

[String[]]$aWMIBinaries=@("unsecapp.exe","wmiadap.exe","wmiapsrv.exe","wmiprvse.exe","scrcons.exe")
foreach ($sWMIPath in @(($ENV:SystemRoot+"\System32\wbem"),($ENV:SystemRoot+"\SysWOW64\wbem"))){
	if(Test-Path -Path $sWMIPath){
		push-Location $sWMIPath
		foreach($sBin in $aWMIBinaries){
			if(Test-Path -Path $sBin){
				$oCurrentBin=Get-Item -Path  $sBin
				Write-Host " Register $sBin"
				& $oCurrentBin.FullName /RegServer
			}
			else{
				# Warning only for System32
				if($sWMIPath -eq $ENV:SystemRoot+"\System32\wbem"){
					Write-Warning "File $sBin not found!"
				}
			}
		}
		Pop-Location
	}
}

Reregister WMI Managed Objects
Continue reading Windows: Powershell script to fix and repair WMI