powershell-2.0

Background job to refresh data on a form while script is running?

久未见 提交于 2019-12-01 21:30:01
Hope some brave will help me ! I’m working from a few days now on a Powershell tool that display a dashboard on the screen corner to give some network “real-time” diagnostics of the computer. This dashboard can be minimized in the notification area. First, I created a function that get the diagnostics and display the status on the form. I tried to refresh data with a timer (See: Real Time Data With Powershell GUI ). The problem was that my function took too much time to be executed and it froze the interface. So buttons on the form were no more usable… (See: Mouse event don't work after first

How to call an executable with parameters from powershell script

醉酒当歌 提交于 2019-12-01 18:26:06
问题 I am seeking help on how to call cmd with specific parameters from a powershell script. So far what I have written is below, but it gives me an error message saying $_cmd is not recognized. I am trying to pass the from date and to date to a an exe... The from date as you can see needs to be today - 1 and the to date should be now. The path of the executable is D:\DataService and that's why I am setting the path early in the script. Write-Host "Get data from service" $path ="D:\DataService"

How to call an executable with parameters from powershell script

∥☆過路亽.° 提交于 2019-12-01 17:51:06
I am seeking help on how to call cmd with specific parameters from a powershell script. So far what I have written is below, but it gives me an error message saying $_cmd is not recognized. I am trying to pass the from date and to date to a an exe... The from date as you can see needs to be today - 1 and the to date should be now. The path of the executable is D:\DataService and that's why I am setting the path early in the script. Write-Host "Get data from service" $path ="D:\DataService" Push-Location $path $Date = Get-Date $DateFrom = $Date.ToString("yyyy-MM-dd HH:mm:ss") $DateTo = $Date

What exception type should be used in Powershell to catch a XML parse error due to invalid characters?

自闭症网瘾萝莉.ら 提交于 2019-12-01 17:14:56
Line 2 in the script below generates - "Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'→', hexadecimal value 0x1A, is an invalid character. Line 39, position 23." At line:1 char:8 + [xml]$x <<<< = Get-Content 4517.xml + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException" What exception should be specified on line 4 (of the script) to catch the aforementioned error? try { [xml]$xml = Get-Content $file # line 2 } catch [?] { # line 4 echo "XML parse error!" # handle the parse error

powershell : ftp directory listing (help on a script)

痴心易碎 提交于 2019-12-01 17:14:54
问题 I am trying to do a list view of a ftp directory. The viewing part is ok so far but I am unable to manipulate the data I am getting back. Here is the script that I used; [System.Net.FtpWebRequest]$ftp = [System.Net.WebRequest]::Create("ftp://ftp.microsoft.com/ResKit/y2kfix/alpha/") $ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory #Details $response = $ftp.getresponse() $stream = $response.getresponsestream() $buffer = new-object System.Byte[] 1024 $encoding = new-object System

Variables imported from the module becomes $null, after the same module imported again

雨燕双飞 提交于 2019-12-01 16:46:22
I have a simple module: varExp.psm1 $var1 = 20 Export-ModuleMember -Variable var1 And I import this module into PS session: PS> Import-Module .\varExp.psm1 then PS> $var1 20 But after I import it second time PS> Import-Module .\varExp.psm1 PS> $var1 PS> $var1 becomes null... Anybody knows what is going on here? (PS2.0) Edit: There are workarounds: Forcing reloading with Import-Module .\varExp.psm1 -Force , and testing if module was loaded before: if(-not (Get-Module varExp)) { Import-Module .\varExp.psm1 } . But I was hoping to get some reason behind $null value in simple case. I don't know

What exception type should be used in Powershell to catch a XML parse error due to invalid characters?

て烟熏妆下的殇ゞ 提交于 2019-12-01 16:41:27
问题 Line 2 in the script below generates - "Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'→', hexadecimal value 0x1A, is an invalid character. Line 39, position 23." At line:1 char:8 + [xml]$x <<<< = Get-Content 4517.xml + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException" What exception should be specified on line 4 (of the script) to catch the aforementioned error? try { [xml]$xml = Get

How to run PowerShell scripts via automation without running into Host issues

寵の児 提交于 2019-12-01 16:31:18
I'm looking to run some powershell scripts via automation. Something like: IList errors; Collection<PSObject> res = null; using (RunspaceInvoke rsi = new RunspaceInvoke()) { try { res = rsi.Invoke(commandline, null, out errors); } catch (Exception ex) { LastErrorMessage = ex.ToString(); Debug.WriteLine(LastErrorMessage); return 1; } } the problem I'm facing is that if my script uses cmdlets such as write-host the above throws an System.Management.Automation.CmdletInvocationException - Cannot invoke this function because the current host does not implement it. What are some good options for

How to test writing to a file share path using credential?

大兔子大兔子 提交于 2019-12-01 16:28:33
I have an array of Credential objects and I would like to test that these credentials have permissions to write a file to a file share. I was going to do something like $myPath = "\\path\to\my\share\test.txt" foreach ($cred in $credentialList) { "Testing" | Out-File -FilePath $myPath -Credential $cred } but then I discovered that Out-File doesn't take Credential as a parameter. What's the best way to solve this? You can use New-PSDrive: $myPath = "\\path\to\my\share" foreach ($cred in $credentialList) { New-PSDrive Test -PSProvider FileSystem -Root $myPath -Credential $Cred "Testing" | Out

PowerShell: how to get if else construct right?

会有一股神秘感。 提交于 2019-12-01 16:28:19
I'm trying to learn powershell and tried to construct a if else statement: if ((Get-Process | Select-Object name) -eq "svchost") { Write-Host "seen" } else { Write-Host "not seen" } This ends up into "not seen", although there is svchost processes. How to modify this to get correct results? Your if-else construct is perfect, but change the if condition like below: (Get-Process | Select-Object -expand name) -eq "svchost" Initially you were comparing an object to the "svchost" which will evaluate to false. With the -expandProperty flag, you are getting that property of the object, which is a