def Get_Sut_Driver_Version(self, nic_name): dict = {} for nic in nic_name: f = open(common.ps_script_path, 'w+') f.write("$secpwd = ConvertTo-SecureString " + self.__sys_password + " -AsplainText -Force" + '\n') f.write( "$cred = New-Object System.Management.Automation.PSCredential ('Administrator',$secpwd)" + '\n') f.write("Invoke-Command -Computer " + self.__sys_ip + " -Scriptblock {" + '\n') f.write( "ForEach ($Adapter in (Get-CimInstance Win32_NetworkAdapter)){" + '\n') f.write("$deviceid = $Adapter.PNPDeviceID" + '\n') f.write("$name = $Adapter.Name" + '\n') f.write("if($name -match \'" + nic + "\' ){" + '\n') f.write( "$Info=Get-WMIObject Win32_PNPSignedDriver|Where-Object {$_.DeviceID -eq $deviceId}" + '\n') f.write("return $Info.driverversion}}} -Credential $cred" + '\n') f.close() cmd = "powershell.exe " + common.ps_script_path (stdout, stderr) = exec_proc.run_cmd(cmd) stdout = stdout.strip('\r\n') # remove ps script cmd = "del " + common.ps_script_path exec_proc.run_cmd(cmd) dict[nic] = stdout print dict return dict
def _Execute_PSScript(self): stdout = None # stdout: output of cmd result stderr = None # stderr: error message. If the cmd executes successfully, stderr will be empty. try: cmd = "powershell.exe " + common.ps_script_path (stdout, stderr) = exec_proc.run_cmd(cmd) cmd = "del " + common.ps_script_path exec_proc.run_cmd(cmd) except Exception, e: print "Fail to Execute PSScript': " + str(e)
def UnInstall_Driver(self, file_name): pnpdeviceid_list = self.Get_Nic_Pnpdeviceid() for deviceid in pnpdeviceid_list: f = open(common.ps_script_path, 'w+') f.write("$exe = \"C:\\PSTools\\psexec.exe\"" + '\n') f.write("&$exe \\\\" + self.__sys_ip + " -u Administrator -p " + self.__sys_password + " -c -f C:\\PSTools\\devcon /r remove \"@" + deviceid + "\"" + '\n') f.close() print deviceid cmd = "powershell.exe " + common.ps_script_path exec_proc.run_cmd(cmd) return common.PASS
def Get_Nic_Pnpdeviceid(self): f = open(common.ps_script_path, 'w+') f.write("$secpwd = ConvertTo-SecureString " + self.__sys_password + " -AsplainText -Force" + '\n') f.write( "$cred = New-Object System.Management.Automation.PSCredential ('Administrator',$secpwd)" + '\n') f.write("Invoke-Command -Computer " + self.__sys_ip + " -Scriptblock {" + '\n') f.write( "ForEach ($Adapter in (Get-CimInstance Win32_NetworkAdapter)){" + '\n') f.write( "$Config = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter \"Index = '$($Adapter.Index)'\"" + '\n') f.write("if(!$Config.IPAddress){" + '\n') f.write("$ipv4 = $Config.IPAddress}" + '\n') f.write("else{" + '\n') f.write("$ipv4 = $Config.IPAddress[0]}" + '\n') f.write("if($ipv4){" + '\n') f.write("if($ipv4 -notmatch " + self.__sys_ip + " ){" + '\n') f.write("$pnpdeviceid= $Adapter.PNPDeviceID + " " + $pnpdeviceid}" + '\n') f.write("else{" + '\n') f.write( "$pnpdeviceid= $pnpdeviceid + $Adapter.PNPDeviceID + \" \"}}}" + '\n') f.write("return $pnpdeviceid} -credential $cred" + '\n') f.close() cmd = "powershell.exe " + common.ps_script_path (stdout, stderr) = exec_proc.run_cmd(cmd) pnpdeviceid_list = String.String().split_string(stdout) # remove ps script cmd = "del " + common.ps_script_path exec_proc.run_cmd(cmd) return pnpdeviceid_list
def Get_Os_Version(self): f = open(common.ps_script_path, 'w+') f.write("$secpwd = ConvertTo-SecureString " + self.__sys_password + " -AsplainText -Force" + '\n') f.write( "$cred = New-Object System.Management.Automation.PSCredential ('Administrator',$secpwd)" + '\n') f.write("(Get-WmiObject -comp " + self.__sys_ip + " -Credential $cred -class Win32_OperatingSystem ).Version" + '\n') f.close() cmd = "powershell.exe " + common.ps_script_path (stdout, stderr) = exec_proc.run_cmd(cmd) stdout = stdout.strip('\r\n') print stdout os_distro = self.Verify_Os_Version(stdout) # remove ps script cmd = "del " + common.ps_script_path exec_proc.run_cmd(cmd) return os_distro
def Check_Connection(self): power_shell_script_content = ( '$service = Get-Service -ComputerName \'%s\' -name winrm\n' + 'while(($service.status) -ne \"Running\"){\n' + 'start-sleep -s 5\n' + '$service = Get-Service -ComputerName \'%s\' -name winrm\n' + '$service.status}\n') % (self.__sys_ip, self.__sys_ip) print(power_shell_script_content) self._Create_PSScript(False) self._Append_PSScript(power_shell_script_content) cmd = "powershell.exe " + common.ps_script_path (stdout, stderr) = exec_proc.run_cmd(cmd) #if len(stdout) == 0 or 'fail' in stderr or 'error' in stderr: if 'fail' in stderr or 'error' in stderr: result = common.FAIL else: result = common.PASS return result
def Install_Driver(self, driver_path, media_host, force): #remove duplicate driver path in list driver_path = list(set(driver_path)) for path in driver_path: print path file = os.path.basename(path) file_name = os.path.splitext(file)[0] media_path = media_host + path print file print file_name print media_path f = open(common.ps_script_path, 'w+') f.write("$url = \"" + media_path + "\"" + '\n') f.write("$output = \"C:\\" + file + "\"" + '\n') f.write("$start_time = Get-Date" + '\n') f.write("Invoke-WebRequest -Uri $url -OutFile $output" + '\n') f.write( "Write-Output \"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)\"" + '\n') f.close() cmd = "powershell.exe " + common.ps_script_path exec_proc.run_cmd(cmd) # remove ps script cmd = "del " + common.ps_script_path exec_proc.run_cmd(cmd) f = open(common.ps_script_path, 'w+') f.write("$dest = \"\\\\" + self.__sys_ip + "\\C$\"" + '\n') f.write("$pwd = ConvertTo-SecureString " + self.__sys_password + " -AsPlainText -Force" + '\n') f.write( "$cred = new-object System.Management.Automation.PSCredential ('Administrator', $pwd)" + '\n') f.write( "New-PSDrive -Name J -PSProvider FileSystem -Root $dest -Credential $cred" + '\n') f.write("Copy-Item -Path \"C:\\" + file + "\" -Destination \"J:\\\"" + '\n') f.close() cmd = "powershell.exe " + common.ps_script_path exec_proc.run_cmd(cmd) # remove ps script cmd = "del " + common.ps_script_path exec_proc.run_cmd(cmd) f = open(common.ps_script_path, 'w+') f.write("$secpwd = ConvertTo-SecureString '" + self.__sys_password + "' -AsplainText -Force" + '\n') f.write( "$cred = New-Object System.Management.Automation.PSCredential ('Administrator',$secpwd)" + '\n') f.write("Invoke-Command -ComputerName '" + self.__host_name + "' -ScriptBlock { Start-Process -Wait -FilePath C:\\" + file + " -ArgumentList '/S','/f' } -Credential $cred" + '\n') f.close() cmd = "powershell.exe " + common.ps_script_path (stdout, stderr) = exec_proc.run_cmd(cmd) # remove ps script cmd = "powershell.exe " + common.ps_script_path exec_proc.run_cmd(cmd) stdout = common.PASS return stdout, file_name