powershell-2.0

How to capture the exception raised in the scriptblock of start-job?

折月煮酒 提交于 2019-11-27 14:30:29
问题 I have the following script, $createZip = { Param ([String]$source, [String]$zipfile) Process { echo "zip: $source`n --> $zipfile" throw "test" } } try { Start-Job -ScriptBlock $createZip -ArgumentList "abd", "acd" echo "**Don't reach here if error**" LogThezippedFile } catch { echo "Captured: " $_ | fl * -force } Get-Job | Wait-Job Get-Job | receive-job Get-Job | Remove-Job However, the exception raised in another powershell instance cannot be captured. What's the best way to capture the

PowerShell 2.0: Accessing Windows Shares during a Remote Session

混江龙づ霸主 提交于 2019-11-27 14:28:49
问题 I am having trouble accessing a shared network location while within a PowerShell remote session. From the PowerShell prompt, I enter a new session: Enter-PSSession server1 The session is properly created and entered. I then attempt to list the contents of the share: dir \\server2\share1 The response is this error: Get-ChildItem : Cannot find path '\\server2\share1' because it does not exist. However, if I remote desktop into server1, bring up PowerShell, and execute the very same dir command

Gracefully stopping in Powershell

試著忘記壹切 提交于 2019-11-27 14:27:44
问题 How do I catch and handle Ctrl + C in a PowerShell script? I understand that I can do this from a cmdlet in v2 by including an override for the Powershell.Stop() method, but I can't find an analog for use in scripts. I'm currently performing cleanup via an end block, but I need to perform additional work when the script is canceled (as opposed to run to completion). 回答1: You could use the method described on here on PoshCode Summary: Set [console]::TreatControlCAsInput = $true then poll for

How to get hash table key which has specific value?

自作多情 提交于 2019-11-27 13:50:43
问题 I am having a hash table where Keys are being used based on value. For ex. $ComponentTobeBuild=@{"ComponentNameX"="True"; "ComponentNameXyz"="False"; "SomeComponent"="False"} I would like to get the keys which are having values True. (I will pass the key to some other script as parameter). I was trying like that , But i think some where i am missing as it is not listing the keys. $($ComponentToBuild.Keys) | Where-Object { $_.Value -eq "True" } How to get the component Name which are having

How to write a custom powershell host [closed]

被刻印的时光 ゝ 提交于 2019-11-27 12:32:49
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Similar to nuget Looking for any starter material hopefully before delving into the debugger 回答1: MSDN has a section devoted to writing a PowerShell host in the PowerShell SDK documentation, which is a nice starting point. Besides that a search returns the following: http://powershellstation.com/2009/10/12

How do you support PowerShell's -WhatIf & -Confirm parameters in a Cmdlet that calls other Cmdlets?

限于喜欢 提交于 2019-11-27 12:14:08
问题 I have a PowerShell script cmdlet that supports the -WhatIf & -Confirm parameters. It does this by calling the $PSCmdlet.ShouldProcess() method before performing the change. This works as expected. The problem I have is that my Cmdlet is implemented by calling other Cmdlets and the -WhatIf or -Confirm parameters are not passed along to the Cmdlets I invoke. How can I pass along the values of -WhatIf and -Confirm to the Cmdlets I call from my Cmdlet? For example, if my Cmdlet is Stop

What security setting is preventing Remote PowerShell 2.0 from accessing UNC paths

≯℡__Kan透↙ 提交于 2019-11-27 12:12:36
This is just crazy, I am starting on PowerShell. And of course I need to do Admin work remotely. A simple dir \\server\share\folder Just refuses to work, I get this error Get-ChildItem : Cannot find path '\\server\share\folder' because it does not exist. + CategoryInfo : ObjectNotFound: (\\server\share\folder:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand To me it is pretty obvious it is an access rights issue. And we do have a domain here at the company. I am logged in to the server, with the exact same

Count items in a folder with PowerShell

放肆的年华 提交于 2019-11-27 11:59:54
I'm trying to write a very simple PowerShell script to give me the total number of items (both files and folders) in a given folder ( c:\MyFolder ). Here's what I've done: Write-Host ( Get-ChildItem c:\MyFolder ).Count; The problem is, that if I have 1 or 0 items, the command does not work---it returns nothing. Any ideas? Stanley De Boer You should use Measure-Object to count things. In this case it would look like: Write-Host ( Get-ChildItem c:\MyFolder | Measure-Object ).Count; or if that's too long Write-Host ( dir c:\MyFolder | mo).Count; and in PowerShell 4.0 use the measure alias instead

How do I retrieve the available commands from a module?

旧时模样 提交于 2019-11-27 11:38:21
问题 To know which PowerShell modules are available on a machine I use the command Get-Module -ListAvailable This returns a list with module-type, -name and the exported commands. But the exported commands are always empty and just displaying {} . Why is this not displayed? Do I have to use another parameter or is there another cmdlet or method to retrieve the available commands? 回答1: Exported commands are not available if the module is not loaded. You need to load the module first and then

Powershell - Reboot and Continue Script

天大地大妈咪最大 提交于 2019-11-27 11:14:54
I'm looking for a way to continue a Powershell script from where it left off after calling a reboot in the script. For example, I am building a DC via Powershell automation, and after renaming the PC to TESTDC01, need to reboot, but after the reboot, continue with the script to go on to dcpromo etc. Is this possible? Cheers! There is a great article on TechNet from the Hey, Scripting Guy series that goes over a situation very similar to what you are describing: Renaming a computer and resuming the script after reboot. The magic is to use the new workflows that are part of version 3: workflow