Chapter5: Common Operation
This section contain the following items:
1.Get disks information
2.Remove item
3.Sleep
4.Timer
5.Launch .exe form .ps1
6.Check if 64bit operation system
7.Get current dictionary
8.Use DISM to add/remove Windows components
9.Write log to text file
10.Check file exist or not
11.Check Network IP
12.Check Network connection type
1ˇ.Check Windows User/Password is valid
1.Get disks information
Unknown (0)
No Root Directory (1)
Removable Disk (2)
Local Disk (3)
Network Drive (4)
Compact Disc (5)
RAM Disk (6)
1.wmic logicaldisk
2.example:
GET-WMIOBJECT -query "SELECT * from win32_logicaldisk where DriveType = '3'"
2.Remove item
1.remove a file
Remove-Item c:\scripts\test.txt
2.remove files under a specific folder:
Remove-Item c:\scripts\#
3.Sleep
start to sleep for 60 seconds:
start-sleep -Seconds 60
4.Timer
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
write-host "Started at $(get-date)"
sleep 20
write-host "Ended at $(get-date)"
write-host "Total Elapsed Time: $($elapsed.Elapsed.ToString())"
5.Launch .exe form .ps1
Launch .exe with parameters
start-process -filepath "$serverDrive\$serverShareFolder\uartecho.exe" -ArgumentList $ary[$i],"0",$Baurate,"1"
start-process: 開啟一個thread並執行指定的程式
-filepath : 指定的程式路徑
-ArgumentList : 給程式的參數
Launch multiple executable files at once
有兩種方式:
1.start process:
先用start-process launch後再用Wait-Process
$p = Start-Process -FilePath iperf.exe -ArgumentList "-s -u" $id=(get-process iperf).Id Wait-Process -id $id -timeout 20 taskkill /T /F /PID $id
2.System.Diagnostics.Process
有時候使用第一種方式會wait失敗, 這邊可以使用第二種方式, 類似c#
$myArray = @() $ary = $ports.Split(","); $length = $ary.length; for($i=0;$i -lt $length; $i++){ $port = $ary[$i] $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = "$serverDrive\$serverShareFolder\uartecho.exe" $pinfo.Arguments="$port 0 $Baurate 1" $pinfo.CreateNoWindow = $false $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $myArray += $p } for($i=0;$i -lt $length; $i++){ $myArray[$i].Start() | Out-Null } for($i=0;$i -lt $length; $i++){ $myArray[$i].WaitForExit() }
execute exe without windows and wait
6.Check if 64bit operation system
1.Using the Get-WMiObject Cmdlet
(gwmi win32_operatingsystem | select osarchitecture).osarchitecture
2.Using powershell command
$version = [System.Environment]::OSVersion.Version $os = "10" if($version.major -eq "6"){ $os = "7" }elseif($version.major -eq "10" -and $version.minor -eq "0" -and $version.build -eq "10240" -and $version.revision -eq "0" ){ #Windows 10 ENT LTSB 64bit $os = "10" }else{ #Default Windows 10 ENT LTSB 64bit $os = "10" }
7.Get current dictionary
Get-Location
8.Use DISM to add/remove Windows components
Dism /online /Enable-Feature /FeatureName:IIS-WebServer /All /quiet /norestart
9.Write log to text file
$domain>> "C:\Windows\System32\domain.txt"
10.Check file exist or not
Test-Path c:\scripts\test.txt
11.Check Network IP
$networks = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .
12.Check Network connection type
$NLMType = [Type]::GetTypeFromCLSID('DCB00C01-570F-4A9B-8D69-199FDBA5723B')
$INetworkListManager = [Activator]::CreateInstance($NLMType)
$NLM_ENUM_NETWORK_CONNECTED = 3
$NLM_NETWORK_CATEGORY_PUBLIC = 0x00
$NLM_NETWORK_CATEGORY_PRIVATE = 0x01
$UNIDENTIFIED = "Unidentified network"
$INetworks = $INetworkListManager.GetNetworks($NLM_ENUM_NETWORK_CONNECTED)
13.Check Windows User/Password is valid
1.Use Get-Credential
Try { Start-Process -FilePath cmd.exe /c -Credential ($C =Get-Credential) $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($C.Password) $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) return 0 } Catch { return -1 }
2.Do not use Get-Credential
try { $passwd = convertto-securestring -AsPlainText -Force -String $password $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$domain\$username",$passwd Start-Process -FilePath cmd.exe /c -Credential ($cred ) return 0 } Catch { return -1 }
Last updated
Was this helpful?