PowerShell
  • Introduction
  • Chapter1: Write a basic powershell script
    • 1.1.Parameters
  • Chapter2: Remote controlling
    • 2.1.Introduction
    • 2.2.System Requirement
    • 2.3.Environment Setting
    • 2.4.Remote login
    • 2.5.Run remote program
    • 2.6.Remote interaction patterns
  • Chapter3: Some remote interactions for Windows 7\/ 10
    • 3.1.USB throughtput interactions
    • 3.2.mSATA throughtput interactions
    • 3.3.UART-echo interactions
    • 3.4.UART-throughput interactions
    • 3.5.UART-loopback interactions
    • 3.6.Ethernet-IIS interactions
    • 3.7.Ethernet-Wake on LAN interactions
    • 3.8.Ethernet-throughput interactions
    • 3.9.Ethernet-static ip interactions
    • 3.10.Ethernet-VPN interactions
    • 3.11.run once interactions
    • 3.12.Time zone interactions
    • 3.13.Firewall interactions
    • 3.14.Watchdog interactions
    • 3.15.File System interactions
    • 3.16.NTP interactions
    • 3.17.SNMP interactions
    • 3.18.FBWF interactions
    • 3.19.EWF interactions
    • 3.20.UWF interactions
  • Chapter4: Common Type
  • Chapter5: Common Operation
  • Chapter6: Socket Programming
Powered by GitBook
On this page
  • 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
  • 13.Check Windows User/Password is valid

Was this helpful?

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()
              }
        1. 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
          }
PreviousChapter4: Common TypeNextChapter6: Socket Programming

Last updated 5 years ago

Was this helpful?