Blog Archives

Wake-On-Lan with PowerShell

Here is a code snippet from PowerShell script that I found on the net (thanks Brandon!) that actually works, as opposed to many examples I tried out that did not send magic packet correctly…

function Send-WOL{
    param ($macaddy)
    $mymac = $macaddy.split(':') | %{ [byte]('0x' + $_) }
    if ($mymac.Length -ne 6)
    {
        throw 'Mac Address Must be 6 hex Numbers Separated by : or -'
    }
    Write-Verbose "Creating UDP Packet"
    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 6)
    $packet += $mymac * 16
    Write-Verbose ([bitconverter]::tostring($packet))
    [void] $UDPclient.Send($packet, $packet.Length)
    Write-Host  "   - Wake-On-Lan Packet of length $($packet.Length) sent to $mymac"
}

…and here is an example:

PS E:\> Send-Wol(’70:f3:95:15:00:b5′)
– Wake-On-Lan Packet of length 102 sent to 112 243 149 21 0 181
PS E:\>

How to get SID with PowerShell

Here is simple example of how to retrieve local NT account (user or group) SID with powershell:

PS E:\TEST> $objUser = New-Object System.Security.Principal.NTAccount("alesk")
PS E:\TEST> $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
PS E:\TEST> $strSID.Value
S-1-5-21-1384281309-654973799-88281384-1000

To retrieve SID of domain user:

PS E:\TEST> $objUser = New-Object System.Security.Principal.NTAccount("acmedomain\alesk")
or
PS E:\TEST> $objUser = New-Object System.Security.Principal.NTAccount("acmedomain","alesk")

PS E:\TEST> $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
PS E:\TEST> $strSID.Value
S-1-5-21-2377153150-1065022559-2428809875-3485

I found this tip among tips published on MS Technet Windows PowerShell tips.

How to convert dynamic disk to basic

Here is a short memo of how you can convert dynamic disk to basic from command line:

cmd> diskpart

DISKPART> list disk

DISKPART> select disk 0

DISKPART> detail disk

-- ------------------------------------------
-- note volume numbers on selected disk
-- and then delete all volumes from the disk
-- ------------------------------------------

DISKPART> select volume 3
DISKPART> delete volume

DISKPART> select volume 4
DISKPART> delete volume 

...
...

DISKPART> select volume 5  <-- last volume on disk 0
DISKPART> delete volume

-- ------------------------------------------
-- now, you can convert dynamic disk to basic
-- ------------------------------------------

DISKPART> select disk 0
DISKPART> convert basic

Replacement for net send (msg.exe)

This is a reminder on how can we send messages to Windows7/2008 after Microsoft deprecated net send mechanism.
Here I want to send message from my Windows XP SP3 workstation to Windows 7 SP1 machine. I’ll be using with msg.exe, the replacement for net send.

E:\>msg /?
Send a message to a user.

MSG {username | sessionname | sessionid | @filename | *}
    [/SERVER:servername] [/TIME:seconds] [/V] [/W] [message]

  username            Identifies the specified username.
  sessionname         The name of the session.
  sessionid           The ID of the session.
  @filename           Identifies a file containing a list of usernames,
                      sessionnames, and sessionids to send the message to.
  *                   Send message to all sessions on specified server.
  /SERVER:servername  server to contact (default is current).
  /TIME:seconds       Time delay to wait for receiver to acknowledge msg.
  /V                  Display information about actions being performed.
  /W                  Wait for response from user, useful with /V.
  message             Message to send.  If none specified, prompts for it
                      or reads from stdin.

My first attempt failed with error:

This was executed on my XP workstation trying 
to send message to "me" logged on mywindows7 PC:
E:\>msg alesk /server:mywindows7  "I need some coffee!!"
Error opening Terminal server kavsek7
Error [1717]:The interface is unknown.

The solution is to allow remote RPC execution on MYWINDOWS7 machine. On MYWINDOWS7 I opened Registry editor and changed AllowRemoteRPC from 0 to 1:

HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server
Name : AllowRemoteRPC
Type : REG_DWORD
Value : 1

Of course, it’s completely up to you to decide if you can live with open RPC port.

Nano on Windows x86/x64

My favorite port of OpenSSH for Windows OS is CopSSH, basically I’m running CopSSH more or less on every production Windows server. One bugger so far was the lack of decent terminal editor that would work out of the box after CopSSH installation. Recently, I started to consolidate different versions of CopSSH (the oldest being 1.4.6) and was happy when I discovered that my favorite editor on Linux, nano 2.2.6, works fine under cygwin environment that ships with the latest CopSSH 4.0.2. All that you need to do is to:

  • download and install CopSSh from Copssh_4.0.2_Installer.zip
  • download and install Control Panel update from ICW_COPSSHCP_2.0.3_installer.zip
  • download nano-2.2.6.zip
  • copy nano.exe from nano-2.2.6.zip archive to CopSSH bin directory, for example copying nano.exe from my local machine to Windows x64 server with scp:

  • scp nano.exe alesk@winbox:"/cygdrive/c/Program\ Files\ \(x86\)/CopSSH/bin"

    That’s it. You can now edit text files without the need to bypass (with WinSCP for example) ssh terminal session just for the sake of editing some configuration file.

Nano doesn’t work with older releases of cygwin that ships with CopSSH (for example on 1.4.6) due to the version mismatch of cygwin1.dll library. I didn’t bother to look for a workaround, since my objective is to deploy the latest version of CopSSH anyhow.