PowerShell scripting with logical conditions
hello,
i completley new powershell need script make. script trying make following virtual machine running under hyper-v on server 2012 or windows 8
- check path of drive which is mounted vm on scsi controller @ specified controller location
- check if path matches path vhd 1 of 2 usb drives (only 1 usb drive attached host 1 time, either have drive letter y or x, or can use drive guids?)
- if path vhd on vm the same path vhd on connected usb drive, usb drive has not yet been swapped, exit script
- if path vhd on vm is different of vhd on connected usb drive usb drive has been swapped, change vhd attached vm of usb drive
i havent got clue variables or conditional statements in powershell, far have following 2 commands.... when run first 1 produces table, 1 of columns path guessing need store in variable , compare usb drive connected....
get-vm test | get-vmharddiskdrive -controllertype scsi -controllernumber 0
this following command job of replacing of the currently attached vhd y drive....
set-vmharddiskdrive -vmname test –controllertype scsi -controllernumber 0 -controllerlocation 0 -path "y:\myvhds\drive1.vhdx"
the command below change vhd on drive x....
set-vmharddiskdrive -vmname test –controllertype scsi -controllernumber 0 -controllerlocation 0 -path "x:\myvhds\drive2.vhdx"
can make sense of , wizzy magic make mess turn neat , tidy :)?
thank help
i'm not code professional, looks pretty straightforward me. efficiencies, there minor possiblities:
- you move first line down before 'if($currentpath -eq $attacheddrive)' line. since have exit condition checking $attacheddrive in first if/else block, save cycles of get-vm command in cases. how depends on how long runs (you can use measure-object obtain that) , how have vm no drive attached.
- you can alter second if statement 'if($currentpath -ne $attacheddrive)' , away block exit statement. if use exit statements method of controlling flow of script, can make difficult incorporate larger processes. in standalone scripts, it's matter of preference.
Windows Server > Windows PowerShell
Comments
Post a Comment