Hviidnet.com
8Jul/080

Nice powershell snippets to do diffrent stuff

Useing powershellplus normaly.

Getting a popup with choices:

$test = [system.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore
$test = [System.Windows.Forms.MessageBox]::Show("test","test",$test)
if ($test -eq "Abort") {echo "operation aborted"}

or:

$test = [System.Windows.Forms.MessageBox]::Show("test","test",[system.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore)
if ($test -eq "Abort") {echo "operation aborted"}

or:

if ([System.Windows.Forms.MessageBox]::Show("test","test",[system.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore) -eq "Abort") {echo "operation aborted"}

other options are YesNo , YesNoCancel and so forth.

Getting hostnames or ipaddresses on the local machine

$ip = [system.Net.Dns]::GetHostByName([System.Net.Dns]::GetHostName()) | Select-Object AddressList
echo $ip

or even better, just get the ip as an array without all the fuss? :

[system.Net.Dns]::GetHostByName([System.Net.Dns]::GetHostName()) | Select-Object AddressList > c:\test.txt
$file = Get-Content c:\test.txt
$ip = @()
foreach ($i in $file)
{
$matches = ""
$i -match "\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if (!$matches){} else {$ip += $matches[0]} | Out-Null
}
echo $ip

Note this script saves it to a file first. There is no reason to do so. (other than me to have both examples if i need it.)