powershell-2.0

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

杀马特。学长 韩版系。学妹 提交于 2019-12-01 16:10:45
问题 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? 回答1: You can use New-PSDrive: $myPath = "\\path\to\my\share" foreach ($cred in

Calculating SHA1 hash algorithm in PowerShell V2.0

a 夏天 提交于 2019-12-01 15:57:32
问题 Is it possible to calculate a SHA-1 hash in PowerShell V2.0? The only information I can find online is with PowerShell V4.0. 回答1: I can't remember back in PowerShell V2 days if .NET 3.5 was typically installed too. I think it's the case. You can always try the following and see if it works: $file = 'd:\scripts\sha1.ps1' $sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider [System.BitConverter]::ToString( $sha1.ComputeHash([System.IO.File]::ReadAllBytes($file))) Replace

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

纵饮孤独 提交于 2019-12-01 15:54:35
问题 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

How to attach a file to an email with PowerShell

六月ゝ 毕业季﹏ 提交于 2019-12-01 15:16:11
I have written a PowerShell script that will create an email, however I can't seem to attach a file. The file does exist and PowerShell can open it, Could anyone tell me what I'm doing wrong? $ol = New-Object -comObject Outlook.Application $message = $ol.CreateItem(0) $message.Recipients.Add("Deployment") $message.Subject = "Website deployment" $message.Body = "See attached file for the updates made to the website`r`n`r`nWarm Regards`r`nLuke" # Attach a file this doesn't work $file = "K:\Deploy-log.csv" $attachment = new-object System.Net.Mail.Attachment $file $message.Attachments.Add(

PowerShell: how to get if else construct right?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 15:10:05
问题 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? 回答1: 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

PowerShell command line parameters and '--'

房东的猫 提交于 2019-12-01 15:06:10
问题 For whatever reason, when I try to call a C# program I'm writing, and I try to pass two arguments with '--' in the command line, PowerShell doesn't call the program with my command line. For instance, I'm providing the command line: .\abc.exe foo.txt -- bar -- When I call this, the C# program's main only gets the command line arguments: foo.txt bar -- instead of foo.txt -- bar -- as would be expected. Why would this be happening? BTW, if I call it as: .\abc.exe foo.txt '--' bar '--' it works

Add text to every line in text file using PowerShell

最后都变了- 提交于 2019-12-01 15:04:27
I'd like to add characters to the end of every line of text in a .txt document. #Define Variables $a = c:\foobar.txt $b = get-content $a #Define Functions function append-text { foreach-Object { add "*" } } #Process Code $b | append-text Something like that. Essentially, load a given text file, add a "*" the the end of every single line of text in that text file, save and close. Soemthing like this should work: function append-text { process{ foreach-object {$_ + "*"} } } Elroy Flynn No function necessary. This would do it: $b|foreach {$_ + "*"} PS> (gc c:\foobar.txt) -replace '\S+$','$&*' Sam

Get-WinEvent Obtain Interactive Logon Messages Only

时光总嘲笑我的痴心妄想 提交于 2019-12-01 10:49:29
I am attempting to get this PS script going to pull the Security log from multiple machines and only search for the Event ID of 4624 and only show me the logs that contain "Logon Type: 2" or interactive logon. I have everything else working except for the part of obtaining only those logs for interactive logon's only. Here is a snip of my script, if anyone has any idea how to get this going it would be greatly appreciated. If I take the 2 out of "Logon Type" it works and I get everything, but if I have anything after that it does not kick any errors, but it doesn't yield results either. Yes, I

Get-WinEvent Obtain Interactive Logon Messages Only

旧巷老猫 提交于 2019-12-01 09:32:36
问题 I am attempting to get this PS script going to pull the Security log from multiple machines and only search for the Event ID of 4624 and only show me the logs that contain "Logon Type: 2" or interactive logon. I have everything else working except for the part of obtaining only those logs for interactive logon's only. Here is a snip of my script, if anyone has any idea how to get this going it would be greatly appreciated. If I take the 2 out of "Logon Type" it works and I get everything, but

Powershell 2 and .NET: Optimize for extremely large hash tables?

心不动则不痛 提交于 2019-12-01 09:26:37
I am dabbling in Powershell and completely new to .NET. I am running a PS script that starts with an empty hash table. The hash table will grow to at least 15,000 to 20,000 entries. Keys of the hash table will be email addresses in string form, and values will be booleans. (I simply need to track whether or not I've seen an email address.) So far, I've been growing the hash table one entry at a time. I check to make sure the key-value pair doesn't already exist (PS will error on this condition), then I add the pair. Here's the portion of my code we're talking about: ... if ($ALL_AD_CONTACTS[