powershell-2.0

PowerShell - Why “Divide By Zero Exception” is not being Caught?

别等时光非礼了梦想. 提交于 2019-11-28 03:44:39
问题 On my Machine each one of the following code snippets throws and exception instead of printing to the standard output "1" and "2" Why the exception is not being Caught? try { [int]$a = 1/0 } catch { write 1 } finally { write 2 } try { [int]$a = 1/0 } catch [System.Exception] { write 1 } finally { write 2 } 回答1: As you are using constants, the interpreter tries to precompute the result and fails with a division by zero error. Your code does not even get executed so there's nothing to trap. You

Monitoring jobs in a PowerShell session from another PowerShell session

◇◆丶佛笑我妖孽 提交于 2019-11-28 03:27:16
问题 A script is executing the following steps in a loop, assume both steps take a long time to complete: $x = DoSomeWork; Start-Job -Name "Process $x" { DoSomeMoreWork $x; }; Step 1 blocks the script and step 2 does not, of course. I can easily monitor the progress/state of the loop and step 1 through the console. What I'd also like to do is monitor the job status of jobs started by step 2 while the batch is still executing. In general, it is possible to 'attach' or query another powershell

How do I get help messages to appear for my Powershell script parameters?

六月ゝ 毕业季﹏ 提交于 2019-11-28 03:15:10
I have a powershell script ( setup.ps1 ), that we use as the entry point for our development environment setup scripts. It takes a parameter: param( [Parameter(Position=0,HelpMessage="The targets to run.")] [Alias("t")] [string[]] $Targets = "Help" ) When I run PS > get-help .\setup.ps1 -detailed in the parameters section, my help message doesn't appear: PARAMETERS -Targets <String[]> What do I need to do to get my parameter help messages to display? You put a certain style of comment at the top of the file that can be decoded by the PowerShell help system. Here's an example: <# .SYNOPSIS .

Basic Powershell - batch convert Word Docx to PDF

让人想犯罪 __ 提交于 2019-11-28 03:09:38
I am trying to use PowerShell to do a batch conversion of Word Docx to PDF - using a script found on this site: http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/24/weekend-scripter-convert-word-documents-to-pdf-files-with-powershell.aspx # Acquire a list of DOCX files in a folder $Files=GET-CHILDITEM "C:\docx2pdf\*.DOCX" $Word=NEW-OBJECT –COMOBJECT WORD.APPLICATION Foreach ($File in $Files) { # open a Word document, filename from the directory $Doc=$Word.Documents.Open($File.fullname) # Swap out DOCX with PDF in the Filename $Name=($Doc.Fullname).replace("docx","pdf") # Save this

How do I convert an array object to a string in PowerShell?

我只是一个虾纸丫 提交于 2019-11-28 02:59:57
How can I convert an array object to string? I tried: $a = "This", "Is", "a", "cat" [system.String]::Join(" ", $a) with no luck. What are different possibilities in PowerShell? Roman Kuzmin $a = 'This', 'Is', 'a', 'cat' Using double quotes (and optionally use the separator $ofs ) # This Is a cat "$a" # This-Is-a-cat $ofs = '-' # after this all casts work this way until $ofs changes! "$a" Using operator join # This-Is-a-cat $a -join '-' # ThisIsacat -join $a Using conversion to [string] # This Is a cat [string]$a # This-Is-a-cat $ofs = '-' [string]$a Jeremy Saunders I found that piping the

Create PSCredential without a password

自闭症网瘾萝莉.ら 提交于 2019-11-28 02:47:29
问题 How to create a instance of PSCredential that has no password? (Without manually filling out a Get-Credential dialog with no password, this is for unattended running.) Things I tried: $mycreds = New-Object System.Management.Automation.PSCredential ("username", $null) Error: Cannot process argument because the value of argument "password" is null $mycreds = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $null -AsPlainText -Force)) Error: ConvertTo

How do I use Add-Type to load Microsoft.Web.Deployment?

不想你离开。 提交于 2019-11-28 02:36:50
问题 I am writing some PowerShell scripts that use the MSDeploy API. I can load the assembly using [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment") The location is in the GAC: PS > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment") | fl Location Location : C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Deployment\7.1.0.0__31bf3856ad364e35\Microsoft.Web.Deployment.dll However, I am not able to load the assembly using Add-Type. I get an error

PowerShell: Capture the output from external process that writes to stderr in a variable

荒凉一梦 提交于 2019-11-28 01:29:34
问题 I need to capture the output of a external process into a variable (string), so I can do some processing on that. The answer from here works nicely as long as the process writes to stdout. However, if the process fails, it writes to stderr. I'd like to capture this string too, and I can't figure out how to do it. Example: $cmdOutput = (svn info) | out-string This works, unless SVN has an error. If an error occured, SVN writes to stderr, and $cmdOutput is empty. How do I capture the text

Using PowerShell to remove lines from a text file if it contains a string

╄→尐↘猪︶ㄣ 提交于 2019-11-28 00:39:57
I am trying to remove all the lines from a text file that contains a partial string using the below PowerShell code: Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt The actual string is H|159|28-05-2005|508|xxx , it repeats in the file multiple times, and I am trying to match only the first part as specified above. Is that correct? Currently I am getting empty as output. Am I missing something? Escape the | character using a backtick get-content c:\new\temp_*.txt | select-string -pattern 'H`|159' -notmatch | Out-File c:\new\newfile.txt

Get Latest Version of Folder from TFS, using Powershell

为君一笑 提交于 2019-11-28 00:38:48
问题 I am trying to " Get Latest Version " of a particular folder from TFS, using Powershell. I have installed the TFS Snappin, and have been using TFS Power Tools cmdlets in PowerShell (such as Get-TfsChildItem and Select-TfsItem etc) [How do I set up TFS PowerShell Snapin ], and have gone through their documentation (which I didn't find explanatory enough!). Confused, on the exact cmdlet to use, when I am trying to get the latest version of an entire Folder structure from TFS, that is mapped to