Hviidnet.com
21Jan/090

Exclude VMware Virtual Adapters from Network Awareness

Because the VMware virtual network adapters appear to be in a “Public network”, Windows thinks that the whole machine is exposed to a public network, and it triggers the public profile for Windows Firewall. While in most cases this helps protect the entire computer from external access, sometimes you actually need to have external access, and therefore you need to manually change the setting.

Make this file: network.ps1 edit it and paste in the following:

# see <a href="http://msdn2.microsoft.com/en-us/library/bb201634.aspx">http://msdn2.microsoft.com/en-us/library/bb201634.aspx</a>
#
# *NdisDeviceType
#
# The type of the device. The default value is zero, which indicates a standard
# networking device that connects to a network.
#
# Set *NdisDeviceType to NDIS_DEVICE_TYPE_ENDPOINT (1) if this device is an
# endpoint device and is not a true network interface that connects to a network.
# For example, you must specify NDIS_DEVICE_TYPE_ENDPOINT for devices such as
# smart phones that use a networking infrastructure to communicate to the local
# computer system but do not provide connectivity to an external network.
#
# Usage: run in an elevated shell (vista/longhorn) or as adminstrator (xp/2003).
#
# PS> .\fix-vmnet-adapters.ps1

# boilerplate elevation check

$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = new-object Security.Principal.WindowsPrincipal $identity
$elevated = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $elevated) {
$error = "Sorry, you need to run this script"
if ([System.Environment]::OSVersion.Version.Major -gt 5) {
$error += " in an elevated shell."
} else {
$error += " as Administrator."
}
throw $error
}

function confirm {
$host.ui.PromptForChoice("Continue", "Process adapter?",
[Management.Automation.Host.ChoiceDescription[]]@("&No", "&Yes"), 0) -eq $true
}

# adapters key
pushd 'hklm:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}'

# ignore and continue on error
dir -ea 0  | % {
$node = $_.pspath
$desc = gp $node -name driverdesc
if ($desc -like "*vmware*") {
write-host ("Found adapter: {0} " -f $desc.driverdesc)
if (confirm) {
new-itemproperty $node -name '*NdisDeviceType' -propertytype dword -value 1
}
}
}
popd

# disable/enable network adapters
gwmi win32_networkadapter | ? {$_.name -like "*vmware*" } | % {

# disable
write-host -nonew "Disabling $($_.name) ... "
$result = $_.Disable()
if ($result.ReturnValue -eq -0) { write-host " success." } else { write-host " failed." }
# enable
write-host -nonew "Enabling $($_.name) ... "
$result = $_.Enable()
if ($result.ReturnValue -eq -0) { write-host " success." } else { write-host " failed." }
}

Next, open a PowerShell prompt. Note that you need to run it with elevated credentials (i.e. "Run as Administrator").

Navigate to the folder where you've placed the script, and execute it. You can type the first letter of the script's name and press TAB to auto complete the script's name.

If you get an error like this:

File D:\Tools\Admin\Scripts\VMware - VMNET Adapters Triggering Public Profile for Windows Firewall\script.ps1 cannot be loaded because the execution of
scripts is disabled on this system. Please see "get-help about_signing" for more details.

you will have to write the following first:

Set-ExecutionPolicy Unrestricted

And wola! stuff works!

For the original article take a look at www.petri.co.il. He does some GREAT work for all windows server stuff.

9Jul/080

Powershell: Scanning a network for alive hosts

This is a way to scan a network for ip's and return if hosts are alive or dead.

$i =1
$Ip = "10.0.0."
$ipsamling = @()
Write-Host "IP Address"
Write-Host "----------------------------------------"
do { $Ip4th = $Ip + $i
$Pingy = get-WmiObject Win32_PingStatus -f "Address='$Ip4th'"
if($Pingy.StatusCode -eq 0) {
"{0,0} {1,5} {2,5}" -f
$Pingy.Address, $Pingy.StatusCode," ON NETWORK"
$ipsamling += $Pingy.Address
}
else
{"{0,0} {1,5} {2,5}" -f $Pingy.Address, $Pingy.StatusCode, " xxxxxxxxx"
}
$i++
}
until ($i -eq 20)

echo "Kontakt til:"
echo $ipsamling

Tagged as: No Comments
9Jul/080

Regex Basics

Here is some basic regex (Regularexpressions)

\d = digit

\d{1,2} = one or 2 digits

\d{3,} = 3 or more digits

\d+ = 1 or more digits

\d* = 0 or more digits

\D = NON digits

\w = word / letters / numbers

\w{3,6} = 3 to 6 letters or numbers

\W = NON words or numbers

\s = matches any white space character, such as tabs, spaces, and so forth.

\S = Any non space chars. space, tabs and so forth.

\\ = \

\. = .

\? = ?

\+ = +

\* = *

\b = word boundary. if i want a word this specifies the boundary. Example \b[Dd]an\b this would find "Dan" even at the start of a line or at "Dan." and only captures "Dan".

^ = marks the start of a line.

$ = ENDS the line (the same as ^, just for the end insted)

example:

"57\\Server2\Share" -match "\\\\\w+\\\w+" (True)
"57\\Server2\Share" -match "^\\\\\w+\\\w+" (False)

first is marked true while the second realises the 57 should not be there.

Its possible to make groups with the following example:

[a-z] = everything between a and z. This still counts as one character, so "a" would be true, and "ab" would be false.

[abc] =  "a" true, "b" true, "c" true, "ab" false and so on.

| = Or. for example "(?:[a-z]+|\d{1,3})/.([a-z]+|\d{1,3})" would capture "0.3" and "a.5"

() = capture group

(?:regex) = the ?: excludes the capture group from the final result.

\d{1,3}? = here ?  means NOT GREEDY so it would only take "1" in "123"

/i = makes the regex match case insensitive
/s = enables "single-line mode". In this mode, the dot matches newlines.
/m = enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string.
/x = enables "free-spacing mode". In this mode, whitespace between regex tokens is ignored, and an unescaped # starts a comment.

Feel free to ask questions below.

8Jul/080

Powershell: A simple RSS Reader

I experimented a bit with reading diffrent data types, and i came up with a simple rss reader :) - Take a look.

#Definer hvorfra det skal hentes
$feed="http://newz.dk/rss"
# hent rss feed
$wco = New-Object System.Net.WebClient
$rss = [xml]$wco.DownloadString($feed)
# Viser det hentede
$rss.rss.channel.item | Select-Object title,pubDate,description | Sort-Object pubDate | Select-Object title,pubDate

Or as a function:

function readRss([string]$feed){
$wco = New-Object System.Net.WebClient
$rss = [xml]$wco.DownloadString($feed)
return $rss.rss.channel.item | Select-Object title,pubDate,description | Sort-Object pubDate | Select-Object title,pubDate
}

readRss "http://www.newz.dk/rss"

Tagged as: No Comments
8Jul/080

Powershell: Create a function

A function in powershell is used as in every other object oriented programming language, by defining what it does and then call it. lets see how to create one:

function Test-Function ($1)
{
$1 += " test"
return $1
}

$test = Test-function "google"
$test.ToString()

The function name here is: "Test-Function" and takes one argument that's called "$1" for this test. Within the loop you will refer to it as "$1" all the way. The value returned to the caller is returned via "return".

To call the function write "Test-function("argument")". replace argument with anything of your choice.

Note it is NOT required for a function to have any arguments!

Another example is a function i used to read a file at match it to a regular expression.

function SearchFile([string]$regex, $fileloc){
$file = Get-Content $fileloc
$array = @()
foreach ($i in $file)
{
$matches = ""
$i -match $regex | Out-Null
if (!$matches){} else {$array += $matches[0]}
}
return $array
}

SearchFile "\d{1,2}.\d{1,2}.\d{1,4}" "c:\10.0.0.144.txt"

Tagged as: No Comments