Category: PowerShell

PS: Windows Update

PS: Windows Update

Needing to run Windows Updates on your machine from PowerShell this is the module for you.

https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc

Download and extract the folder a place it in the following directory

%WINDIR%\System32\WindowsPowerShell\v1.0\Modules

Import the module into your PowerShell session

Import-Module PSWindowsUpdate

To download and install windows updates run the following

Get-WUInstall

Screenshot of Module In action

powershell-windows-update-screenshot

Advertisements
PowerCLI: Rename Windows Guest OS Hostname

PowerCLI: Rename Windows Guest OS Hostname

Okay, today I had run a script but I had not taken into account the character limit of the Windows hostname. The Result was I had 14 VM’s that are all running with the same hostname and I was not prepared to go into every single VM and change the hostname manually.

So the solution, I had been fishing around the internet for a quick solution to this problem and the solution was to use PowerCli with the Invoke-VMscript command which turns out to be pretty powerful so below is an example of using Invoke-VMscript to change the Windows Hostname

Invoke-VMScript -VM VM-Windows-01 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-01 -Restart' -GuestUser Administrator -GuestPassword ChangeMe
Invoke-VMScript -VM VM-Windows-02 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-02 -Restart' -GuestUser Administrator -GuestPassword ChangeMe
Invoke-VMScript -VM VM-Windows-03 -ScriptType Powershell -ScriptText 'Rename-Computer -NewName VM-WIN-03 -Restart' -GuestUser Administrator -GuestPassword ChangeMe

 

PowerCLI: SAN Usage Report

PowerCLI: SAN Usage Report

 

Okay, today I have been tasked to find out where all of our storage has gone and because this SAN LUN is shared between a multiple of systems I have decided to generate the report based on Folder size within the LUN. Please see below for the script to run from PowerCLI

$searchSpec1 = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec1.details = New-Object VMware.Vim.FileQueryFlags
$searchSpec1.details.fileSize = $true
$searchSpec1.query += New-Object VMware.Vim.FolderFileQuery
$searchSpec2 = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec2.details = New-Object VMware.Vim.FileQueryFlags
$searchSpec2.details.fileSize = $true
$searchSpec2.query += New-Object VMware.Vim.FileQuery
$report = @()
Get-Datastore | Get-View | % {
$browser = Get-View -Id ($_.Browser)
$dsPath = "[" + $_.Summary.Name + "] /"
$folderList = $browser.SearchDatastore($dsPath, $searchSpec1)foreach($folder in $folderList.File){
$folderPath = $dsPath + $folder.Path
$fldresult = $browser.SearchDatastore($folderPath, $searchSpec2)
$fldSize = 0
foreach($file in $fldresult.File){$fldSize += $file.FileSize}
$row = "" | select DSName, FolderName, FolderSize
$row.DSName = $folderList.FolderPath
$row.FolderName = $folder.Path
$row.FolderSize = $fldSize
$report += $row
}
}
$report | Export-Csv "c:\powershell\Foldersize.csv" -noTypeInformation

Once you have harvested your CSV import it into excel and use the following on the custom format for file size

[<500000]#,##0" B ";[<500000000]#,##0,," MB";#,##0,,," GB"
PS: GUI Building Made Easy

PS: GUI Building Made Easy

Okay so when it comes to doing form building in raw PowerShell the formatting can be a complete pain in the ass. So I have been searching online that’s pretty dangerous for me I never know what I might find but on this occasion I have found an awesome tool from Sapien Technologies called Primal Forms CE the only issue I wasn’t able to find a download link for the application and it looks like they have discontinued it for free use but don’t worry I managed to find it sitting on an old mirror and I have left a copy of it on my OneDrive for all of you to download.

So what does it do?

In essence it is a simple drag and drop environment for designing your GUI that is then exportable into raw PowerShell code. Saving you the time and the headache of trying to format it in code

Screenshot:

c8353444d582ed3d820731582abf994d

Download Link:

https://1drv.ms/u/s!Amaaew76M7RY1Wo78izJXXDhhSjh

 

PS: Using Forms to Return PC Hostname

PS: Using Forms to Return PC Hostname

The following script is a short demo to show you how to build a form and display the PC host name in a text box on the button click action

#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$textBox1 = New-Object System.Windows.Forms.TextBox
$button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick=
{
#TODO: Place custom script here
$textBox1.Text = $env:COMPUTERNAME

}

$handler_form1_Load=
{
#TODO: Place custom script here

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 261
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "GET PC Name"
$form1.add_Load($handler_form1_Load)

$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 120
$textBox1.Location = $System_Drawing_Point
$textBox1.Name = "textBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 175
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 1

$form1.Controls.Add($textBox1)

$button1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 105
$System_Drawing_Point.Y = 91
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 0
$button1.Text = "GO!"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm