Snippets

 A few PowerShell snippets I use a bit...

# Show Services that are set to Automatic that are not running:

Get-WmiObject win32_service |?{ $_.StartMode -eq 'Auto' -and $_.State -ne 'Running'} | ft -auto

 # and a loop to start them:

while((Get-WmiObject win32_service |?{ $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' }) -ne $null){Get-WmiObject win32_service |?{ $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } | %{start-service $_.name}}

 # check for missing updates pushed by SCCM (null return means no updates pending):

Get-WMIObject -Namespace root\CCM\ClientSDK -Class CCM_SoftwareUpdate -Filter ComplianceState=0

 # and install them and reboot:

$MissingUpdates = Get-WMIObject -Namespace root\CCM\ClientSDK -Class CCM_SoftwareUpdate -Filter ComplianceState=0

$MissingUpdatesReformatted = @($MissingUpdates | Foreach-Object {if($_.ComplianceState -eq 0){[WMI]$_.__PATH}})

$InstallReturn = Invoke-WMIMethod -Namespace root\CCM\ClientSDK –Class CCM_SoftwareUpdatesManager -Name InstallUpdates –ArgumentList (,$MissingUpdatesReformatted)

while((Get-WMIObject -Namespace root\CCM\ClientSDK -Class CCM_SoftwareUpdate -Filter ComplianceState=0 | ?{$_.EvaluationState -lt 8}) -ne $null){write-progress -Activity "Installing updates…" -Status "The server will reboot automatically when all updates have finished installing"; Sleep 60}

shutdown /r /t 60 /f


 # get mail users from AD:

Ipmo activedirectory

$mailusers = get-aduser -filter * -Properties mail,mailnickname,msExchRecipientTypeDetails

$mailusers.count

$path = $env:userprofile + "\Documents\"

$file1 = "mailusers.csv"

$file2 = "mailboxes.csv"

$mailusers | export-csv $path$file1 -NoTypeInformation

 # and mailboxes:

$mailusers | ?{$_.msExchRecipientTypeDetails -eq 1} | export-csv $path$file2 -NoTypeInformation

 # last boot

Gwmi win32_operatingsystem | select @{L=’Boot’;E={$_.ConverttoDateTime($_.lastbootuptime)}}