Powershell

[PowerShell] Send email from Gmail with attachment on PowerShell

Credits goes to 

I modified the script a little bit to pick up the log from c:\temp\ daily where the filename is made up of yyyymmdd

$a = Get-Date -Format yyyyMMdd

$smtpClient = new-object system.net.mail.smtpClient 
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username@gmail.com", "password");

$emailFrom = "sender@domain.com"
$emailTo = "receipt1@domain.com,receipt2@domain.com"
$Subject = "Backup log for " +$a
$Body = "Please refer to attached logfile."

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
$emailMessage.To.Add($EmailTo)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\temp\libra_backup"+ $a+".txt")
$SMTPClient.Send($emailMessage)
$emailMessage.Attachments.Dispose()

 

For more information on Get-Date format, please click here

[PowerShell] Retrieving hard disk health through SMART status

To get the SMART status of the hard disk with PowerShell, you could run the following:

Get-WmiObject win32_diskdrive|Select-Object Status

 

Below is a sample script to check multiple PC’s SMART status by getting a list of PC names from a text file.

$allComputers=Get-Content -Path C:\temp\pc.txt

foreach($computer in $allComputers)
{

$isonline=test-connection -Quiet -count 2 -ComputerName $computer
if($isonline)
{

$diskStatus= Get-WmiObject win32_diskdrive -ComputerName $computer|Select-Object -ExpandProperty Status

Write-Host -ForegroundColor Green "$computer is online and disk status is $diskStatus";
$diskStatus="";
}
else
{
Write-Host -ForegroundColor Red "$computer is offline";
}

}

[PowerShell] Export AD photo

Here is a PowerShell to export AD photo, please make sure you have the rights to do so before running the script. The script will prompt for the username and it will be saved as “username.jpg” under the path specify in $path variable.

 

$username = Read-Host 'Please enter Username'

$path = 'C:\Photos\'

$User=GET-ADUser $username –properties thumbnailphoto

$Filename=$path+$username+'.jpg'

[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)

 

[PowerShell] Retrieve temperature information for Nvidia display cards

Some Nvidia display cards has support for WMI such that you could retrieve the temperature information.

For more information, please check out NVIDIA Windows Management Instrumentation SDK

Below is the code for local computer, you can always change the $ComputerName variable to point to a remote computer. I had it probe for all instances of the ThermalProbe, you can comment that out and un-comment the last 3 lines for temperature only. The majority part of the code comes from Nvidia’s SDK examples, which you could download from the link above.

Please note, as I only have NVS cards, I had filled up $supportedModels with the NVS model I have.

$ComputerName="localhost"
$supportedModels= "NVIDIA NVS 290","NVIDIA NVS 295","NVIDIA NVS 300","NVIDIA NVS 310","NVIDIA NVS 315","NVIDIA NVS 420","NVIDIA NVS 450","NVIDIA NVS 510","NVIDIA Quadro NVS 450"


$video_card_name=(gwmi win32_videocontroller -ComputerName $ComputerName)|select-object -expand name
write-host $video_card_name

If($supportedModels|?{$video_card_name -match $_})
{

#Write-Host "True"
gwmi -ComputerName $ComputerName -Namespace root\CIMV2\NV gpu|select-object name,percent*


$namespace = "root\CIMV2\NV"
$classname = "ThermalProbe"
# For local system (e.g. "LocalHost", ".") '-computername $computer' could be omitted
# retrieve all instances of the ThermalProbe class and store them:
$probes =Get-WmiObject -class $classname -computername $ComputerName -namespace $namespace
# print all ThermalProbe instances
$probes
# iterate through all Probe instances
foreach( $probe in $probes )
{
#"Call the info() method and print all the data"
$res = $probe.InvokeMethod("info",$Null)
$res
#"Query just the temperature"
#$temp = $probe.temperature
#$temp
}
}

[PowerShell] Decommission user on AD with PowerShell

The following script will do the following:

1.prompt you to input the username to be decommission

2. remove all group while writing a backup of the group to a csv

3.Move the user to OU=Decommission,OU=USER,DC=company,DC=local

4. Set Description to “Left on $date”, where $date is current date

5. Disable the account

 

$date= Get-Date -Format dd/MM/yyyy
# Removes user from all AD groups except Domain Users.
$user = Read-Host 'Please enter Username'
Get-ADPrincipalGroupMembership $user | Get-ADGroup -Properties * | select name, description,GroupCategory|Export-Csv $user".csv" -NoTypeInformation
$ADgroups = Get-ADPrincipalGroupMembership -Identity $user | where {$_.Name -ne "Domain Users"}
Remove-ADPrincipalGroupMembership -Identity "$user" -MemberOf $ADgroups -Confirm:$false

Get-ADUser $user|Move-ADObject -TargetPath 'OU=Decommission,OU=USER,DC=company,DC=local'
Set-ADUser $user -Description "Left on $date"
Disable-ADAccount -Identity $user

[PowerShell] Getting list of Windows 8 and 10 from AD with PowerShell

This example uses Get-ADComputer and -Filter to get a list of Windows 8 and 10 from AD.

Get-ADComputer -Filter {(OperatingSystem -LIKE "Windows 8*") -or (OperatingSystem -LIKE "Windows 10*")} -Properties *|Select name, operatingSystem, OperatingSystemVersion|Sort-Object OperatingSystemVersion -Descending|Out-GridView

[PowerShell] Starting,Stopping and Restarting Remote Services with PowerShell

Learn Powershell | Achieve More

A common task that a System Administrator might face in their day is restarting a service on a remote system. While this can be accomplish using the Services.MSC or another various GUI, you have to load the GUI, then connect to the remote system, locate the service and finally perform the action on the service.

There are a few ways that this can be done using WMI (Win32_Services) and using Get-Service along with Stop/Start/Restart-Service cmdlets.

Using WMI

The first method of managing a service that I will go into is using WMI to accomplish this task. First I will connect to the specific service that I want to manipulate, in this case, the windows update client service: wuauserv.

$service = Get-WmiObject -ComputerName DC1 -Class Win32_Service `
-Filter "Name='wuauserv'"
$service

image

We now have our WMI service object for wuauserv. Lets take a look at all of the available methods for…

View original post 721 more words

[PowerShell] Script Lets You Check Patches’ Status

I was asked to modify the script posted at http://windowsitpro.com/scripting/powershell-script-lets-you-check-patches-status

such that given a list of computers, check if any PC has missed a KB. So here it is:

$erroractionpreference = “SilentlyContinue”
clear-host

if ($args.count -lt 2)
{
write-host -f blue “Syntax Error : Must have 2 parameters.”
write-host -f blue “Eg powershell.exe \kb.ps1 ”
break
}

$filename = $args[0]
$computer = $args[1]
$kbs = get-content $filename
$kbTotal = 0
$kbNotFound = 0
write-host -f green “Checking on”$computer

foreach ($kb in $kbs)
{
$global:kbTotal++
$strQuery = “select * from win32_pingstatus where address = ‘” + $computer + “‘”
$wmi = get-wmiobject -query $strQuery
if ($wmi.statuscode -eq 0)
{
$checkkb = get-wmiobject Win32_QuickFixEngineering -computer $computer | where-object {$_.hotfixid -eq $kb} | select-object hotfixid, description
# BEGIN CALLOUT A

if ($checkkb.hotfixid -eq $kb)
{write-host -f green $kb “`t” $checkkb.description “`r”}
else
{write-host -f red $kb “`t” “Patch not found.” “`r”
$global:kbNotFound++
}
}
else
{write-host $computer “`t” “Ping failed.” “`r”}
# END CALLOUT A
}

if ($global:kbNotFound -eq 0)
{write-host -ForegroundColor Green $global:kbNotFound “KBs missing out of”$global:kbTotal “required”}
else
{write-host -ForegroundColor Red $global:kbNotFound “KBs missing out of”$global:kbTotal “required”}

Below is the the version where it outputs to a text file, I had modified it to output to c:\temp instead:

# Function : to check for kbxxxx patches on remote pc
# This version redirects the output to a text file.

$erroractionpreference = “SilentlyContinue”

cls
if ($args.count -ne 2)
{
write-host -f blue “Syntax Error : Must have 2 parameters.”
write-host -f blue “Eg powershell.exe \kb.ps1 ”
break
}

$filename = $args[0]
$computer = $args[1]
$kbs = get-content $filename

write-host -f green “Checking on”$computer
“Checking on” +$computer | out-file c:\temp\results.txt -append

foreach ($kb in $kbs)
{

$strQuery = “select * from win32_pingstatus where address = ‘” + $computer + “‘”
$wmi = gwmi -query $strQuery

if ($wmi.statuscode -eq 0)
{
$checkkb = gwmi Win32_QuickFixEngineering -computer $computer | where {$_.hotfixid -eq $kb} | `
select-object hotfixid, description

if ($checkkb.hotfixid -eq $kb)
{
write-host -f green $kb “`t” $checkkb.description “`r”
$kb + ” ” + $checkkb.description | out-file c:\temp\results.txt -append
}

else
{
write-host -f red $kb “`t” “Patch not found.” “`r”
$kb + ” patch not found” | out-file c:\temp\results.txt -append
}

}
else
{
write-host $computer “`t” “Ping fail.” “`r”
$computer + ” ping fail”| out-file c:\temp\results.txt -append
}

}