Search This Blog


PowerShell Startup Scripts

Set Execution policy so that you can run these scripts with admin rights.

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at Do you want to change the execution policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y

Create a file and populate the contents


# Send "configuring" and "online" status transitions to state tracking server
# Allows us to know whether the machine booted successfully and has network connectivity

# Port number and hostname of state tracking server

Get-Date > C:\startup.log

#### To Be Removed
#$EthernetAdapter=(Get-NetAdapter | where {$_.Status -eq 'Up'}).Name
#Get-NetAdapter >> C:\startup.log
#Write "Enabled Adapter: " $EthernetAdapter >> C:\startup.log

# Hoping that interface naming is done via lowest bus number.
# Lowest Bus# hopefully means control interface (NIC1 in vmware)
if ($EthernetArray.Bus[0] -eq $EthernetArray.Bus[1])
Write "yes"
if ($EthernetArray.Device[0] -lt $EthernetArray.Device[1])
Write $EthernetAdapter >> C:\startup.log
Write $EthernetAdapter >> C:\startup.log

elseif ($EthernetArray.Bus[0] -lt $EthernetArray.Bus[1])
Write $EthernetAdapter >> C:\startup.log
Write $EthernetAdapter >> C:\startup.log

# Test the connection to the waterloo lab dns servers
# Should also be accessible to bangalore.
# Not easy to find a consistent IP to test with before network adapters are setup
do {
Write "Connection Not Ready" >> C:\startup.log
} until (Test-Connection -Quiet -ComputerName
Write "ConnectionReady" >> c:\startup.log

# Grab DNS from what is hopefully the control interface NIC1
$LabDns=(Get-DnsClientServerAddress -InterfaceAlias $EthernetAdapter -AddressFamily IPv4).ServerAddresses[0]
Write "LabDns:" $LabDns >> C:\startup.log

# Get the local control IP from Ethernet which should be the first available NIC
$ControlIp=(get-netadapter -Name $EthernetAdapter | get-netipaddress | ? addressfamily -eq 'IPv4').ipaddress
Write "ControlIp: " $ControlIp >> C:\startup.log
# Use the control IP to do a DNS lookup to find the hostname
$ComputerName = (Resolve-DnsName -Name $ControlIp -Server $LabDns -Type PTR | Where {$_.Type -eq "PTR"}).NameHost -replace '\..*$'
Write "ResourcedIP: " $ResourcedIp >> c:\startup.log
Write "ComputerName: " $ComputerName >> C:\startup.log

# Output the current status to state tracking machine to let it know that machine has booted successfully
Write "setStatus $ComputerName configuring" | & "C:\Program Files (x86)\Nmap\ncat.exe" $ResourcedIp $Port 2>&1 >> C:\startup.log

# Extend the partition size to match the virtual disk image size
$size=(Get-PartitionSupportedSize -DriveLetter C)
Write "Resizing Partition to size: " $size >> C:\startup.log
Resize-Partition -DriveLetter C -Size $size.SizeMax

# Send a final online status to state tracking server to let it know its done
Write "setStatus $ComputerName online" | & "C:\Program Files (x86)\Nmap\ncat.exe" $ResourcedIp $Port 2>&1 >> C:\startup.log

#### To Be Removed
#Turning Disabled interface back on
#$DisabledEthernetAdapter=(Get-NetAdapter | where {$_.Status -ne 'Up'}).Name
#Write "Disabled Interface: " $DisabledEthernetAdapter >> C:\startup.log
#Enable-NetAdapter -Name $DisabledEthernetAdapter

Then you will need to create a startup item for this script which can also be done in powershell

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:01:00
Register-ScheduledJob -Trigger $trigger -FilePath c:\startup.ps1 -Name LabStartupScript
Get-ScheduledJobOption -Id 1 | Set-ScheduledJobOption -RunElevated -RequireNetwork

No comments:

Post a Comment