powershell-4.0

How to check if a program is installed and install it if it is not?

会有一股神秘感。 提交于 2019-11-30 21:12:30
I would rather not use WMI due the integrity check. This is what I have that does not work: $tempdir = Get-Location $tempdir = $tempdir.tostring() $reg32 = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" $reg64 = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" if((Get-ItemProperty $reg32 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null) -Or (Get-ItemProperty $reg64 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null)) { (Start-Process -FilePath

How do I resolve “Cannot invoke the SendConfigurationApply method.” when performing Start-DscConfiguration?

风格不统一 提交于 2019-11-30 12:19:37
Another way to phrase the question less specifically: What is the correct way to 'reset' the DSC processes on a target machine? I've created a DSC configuration that I'm iterating on right now, and I am adding new Package configuration which I've gotten wrong. I determined that by forgetting to supply the /quiet argument to an MSI installer in a Package block I can cause the Start-DscConfiguration cmdlet to 'hang'. At the onset of this 'hang' I stop the DSC configuration operation on my local machine and attempt to correct the configuration problem (by adding the /quiet argument in my example)

How to check if a program is installed and install it if it is not?

馋奶兔 提交于 2019-11-30 04:53:35
问题 I would rather not use WMI due the integrity check. This is what I have that does not work: $tempdir = Get-Location $tempdir = $tempdir.tostring() $reg32 = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" $reg64 = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" if((Get-ItemProperty $reg32 | Select-Object DisplayName | Where-Object { $_.DisplayName -Like '*Microsoft Interop Forms*' } -eq $null) -Or (Get-ItemProperty $reg64 | Select-Object DisplayName |

Setup default date format like yyyy-mm-dd in Powershell?

天涯浪子 提交于 2019-11-30 03:44:58
问题 A simple & short question: How can I setup a default date format in powershell like yyyy-mm-dd ? so any date output will be like this format? or How to setup a date format globally in one script ? Is there a way to output date only without time? when I output LastWriteTime, Default is 13-03-2014 14:51 I only need 13-03-2014 but 14:51 . 回答1: A date in PowerShell is a DateTime object. If you want a date string in a particular format, just use the built-in string formatting. PS C:\> $date = get

How do I resolve “Cannot invoke the SendConfigurationApply method.” when performing Start-DscConfiguration?

爱⌒轻易说出口 提交于 2019-11-29 17:32:25
问题 Another way to phrase the question less specifically: What is the correct way to 'reset' the DSC processes on a target machine? I've created a DSC configuration that I'm iterating on right now, and I am adding new Package configuration which I've gotten wrong. I determined that by forgetting to supply the /quiet argument to an MSI installer in a Package block I can cause the Start-DscConfiguration cmdlet to 'hang'. At the onset of this 'hang' I stop the DSC configuration operation on my local

How can I query a temporary PS-Drive while returning files with a name relative to the drive?

你说的曾经没有我的故事 提交于 2019-11-29 13:15:38
I am trying to create a script where I will be searching the file servers for non inherited permissions. I have run into the 260 character limit for file names as a result. A suggestion I saw, that I thought would help, a couple of times was to create some non persistent PS Drives a couple of levels deep and query those. Problem is when I use Get-ChildItem against the new PS Drives it is returning object with the full network path and not using the name I assigned it. # Cycle the folders Get-ChildItem $rootPath -Directory | select -first 1 | ForEach-Object{ $target = $_ # Create a PS Drive for

Powershell - Regular Expression Multiple Matches

情到浓时终转凉″ 提交于 2019-11-29 11:01:12
Maybe my reasoning is faulty, but I can't get this working. Here's my regex: (Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z)) Try it: http://regex101.com/r/jQ6uC8/6 $getdevice is the input string. I'm getting this string from the Stream/Output from a command line tool. $dstate = $getdevice | select-string -pattern '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' -AllMatches | % { $_ -match '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' > $null; $matches[0] } Write-Host $dstate Output: Device #0 Device #1 Device #2 Device #3 Device #4 Same output for the $matches[1], $matches[2] is empty. Is there a way I can

Launch Elevated CMD.exe from Powershell

╄→гoц情女王★ 提交于 2019-11-29 08:37:45
I am trying to launch an elevated CMD window from PowerShell but I am running into some issues. Below is the Code I have now. There is an admin account on the machine that has the username of "test" and a Password of "test" $username = "test" $password = ConvertTo-SecureString "test" -AsPlainText -Force $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password Start-Process "cmd.exe" -Credential $cred This is all working fine for running an application from the user profile with no administrator rights that this script sits in, but when calling

Invoke-Command faster than the command itself?

有些话、适合烂在心里 提交于 2019-11-29 04:34:01
I was trying to measure some ways to write to files in PowerShell. No question about that but I don't understand why the first Measure-Command statement below takes longer to be executed than the 2nd statement. They are the same but in the second one I write a scriptblock to send to Invoke-Command and in the 1st one I only run the command. All informations about Invoke-Command speed I can find are about remoting. This block takes about 4 seconds: Measure-Command { $stream = [System.IO.StreamWriter] "$PSScriptRoot\t.txt" $i = 0 while ($i -le 1000000) { $stream.WriteLine("This is the line number

Error Handling for Invoke-RestMethod - Powershell

风格不统一 提交于 2019-11-29 01:19:41
I have a powershell script using the Skytap API (REST). I would like to catch the error, if there is one, and try to display it. For example, we are changing the IP: Invoke-RestMethod -Uri https://cloud.skytap.com/configurations/XXXXXX/vms/YYYYYY/interfaces/ZZZZZZ?ip=10.0.0.1 -Method PUT -Headers $headers If the IP is used somewhere else, I will get the 409 Conflict Error (Request is well-formed but conflicts with another resource or permission). I would like to check if the error is 409 and then tell it to do something else about it. This is somewhat awkward but the only way to do it as far