PowerGUI – Remotely Determine if Powershell is Installed

Here is a Node Script to determine if Powershell is installed on a remote computer:

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$name = [Microsoft.VisualBasic.Interaction]::Inputbox(“Enter the IP or Name of the server:”)
$pshell = Get-WmiObject Win32_QuickFixEngineering -ComputerName “$($name)” | Where-Object{$_.HotFixID -eq “kb968930”}
if ($pshell -eq $null){Write-Output “Powershell 2.0 is NOT installed.”}
else {Write-Output “Powershell 2.0 IS installed”}

If you found this useful, subscribe.

Advertisements

Powershell – How to Remove Array Elements

Normally, to remove array elements you simply assign the elements you want to keep back to the array’s name. What is really happening is the array is getting copied to a new array with the same name like this:

$array = $array[0..($i – 1)] + $array[($i – 1)..$array.Count-1]

So here is something strange that I noticed today. If you are looping through an array, based on the length of the array, and remove an element; the loop will get stuck in an infinite loop. Here is an example:

[string[]]$array = (1..10)

for ($i = 0; $i -lt $array.Count; $i += 1){

$array = $array[0..($i – 1)] + $array[($i – 1)..$array.Count-1]

}

From what I can see, it looks like the $array.Count in argument list for the for loop only gets calculated once. When an element is removed, it can never break out of the loop because the condition never gets satisfied. If you were to do this in VB it would error out, but not in Powershell, this is a possible bug they will have to fix in a later release.

If you found this useful, subscribe.

PowerGUI – Using Powershell to get Disk Space

This is a script you can use in PowerGUI as a script node. This handy little script will give you the basic information of your drives so you can figure out if you disks are going to run out of space! I had to make a modification to this script, it works much better now! I apologize to everyone who was using the older script, which works well in Powershell but not PowerGUI.

Note: This will work with your existing credentials.

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$name = [Microsoft.VisualBasic.Interaction]::Inputbox(“Enter the IP or Name of the server:”)

$drives = gwmi win32_logicaldisk -ComputerName $name | where{ $_.drivetype -eq 3 }
$driveArray = @()
$counter = 0
foreach ($drive in $drives){
$driveArray += New-Object -TypeName System.Object
$driveArray[$counter] | Add-Member -MemberType NoteProperty -Name name -Value $drive.Name
$driveArray[$counter] | Add-Member -MemberType NoteProperty -Name percentfree -Value ([int] [System.Math]::Round(($drive.FreeSpace) / $drive.Size * 100))
$driveArray[$counter] | Add-Member -MemberType NoteProperty -Name sizeGB -Value ([int]($drive.Size / 1Gb))
$driveArray[$counter] | Add-Member -MemberType NoteProperty -Name freespaceGB -Value ([int]($drive.FreeSpace / 1Gb))
$counter +=1
}
$driveArray | Format-Table

A slimmed down version for the Powershell console can be found here: Console Script

I also made a system monitoring script, free for you to use.

If you found this useful, subscribe to my blog.

PowerGUI – Powershell SQL Server Database Information Script Node

This short script will prompt you for the name or IP of the server you wish to connect to. As long as you have the correct rights to that server you can use this script. Hope this helps Fernando!

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null
$name = [Microsoft.VisualBasic.Interaction]::Inputbox(“Enter the IP or Name of the server:”)
$server = New-Object(‘Microsoft.SqlServer.Management.Smo.Server’) $name
$server.Databases | format-table Name,Size,RecoveryModel,PrimaryFilePath -AutoSize

If you found this useful, subscribe.