powershell-2.0

http requests with powershell

三世轮回 提交于 2019-11-29 03:16:44
I am looking to make http requests to web pages with powershell, is this possible and if so, how may I achieve this? Can I make requests to https pages? I am able to make http requests with a bat file but not https, was hoping I could https page requests with powershell. You can use the usual WebRequest and HttpWebRequest classes provided by the .NET framework. $request = [System.Net.WebRequest]::Create('http://example.com') # do something with $request It's no different from using the same classes and APIs from C#, except for the syntactic differences to PowerShell. PowerShell v3 also brings

PowerShell: detecting errors in script functions

折月煮酒 提交于 2019-11-29 03:01:35
问题 What is the best way to detect if an error occurs in a script function? I'm looking for a consistent way to indicate error/success status similar to $? (which only works on cmdlets, not script functions). Given a particular function might return a value to be used by the caller, we can't indicate success by returning a boolean. A function could use a [ref] parameter and set the value appropriately inside the function and check after the call, but this is more overhead than I'd like. Is there

Is it possible to create a Database in SQL Server with powershell?

无人久伴 提交于 2019-11-29 02:59:40
问题 I am trying to create a empty database in SQL server using powershell and SMO but cannot seem to find a way of doing it. Is this possible? Connection script for sql server: [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null $serverName = "localhost" $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $serverName $server.ConnectionContext.LoginSecure=$false; $credential = Get-Credential $loginName = $credential.UserName -replace("\\","")

Prevent trailing newline in PowerShell Out-File command

你说的曾经没有我的故事 提交于 2019-11-29 02:59:35
How do I prevent PowerShell's Out-File command from appending a newline after the text it outputs? For example, running the following command produces a file with contents "TestTest\r\n" rather than just "TestTest". "TestTest" | Out-File -encoding ascii test.txt In PowerShell 5.0+, you would use: "TestTest" | Out-File -encoding ascii test.txt -NoNewline But in earlier versions you simply can't with that cmdlet. Try this: [System.IO.File]::WriteAllText($FilePath,"TestTest",[System.Text.Encoding]::ASCII) To complement briantist's helpful answer re -NoNewline : The following applies not just to

How do I find the MSI product version number using PowerShell?

試著忘記壹切 提交于 2019-11-29 01:27:21
问题 Our end deliverable has lot of MSI files. I would ensure whether they has correct product name and product version. I am using Orca and doing it manually. How to do it using PowerShell? 回答1: This should have been an easy answer... To start with Windows Installer has a COM object you can use: ProgID: WindowsInstaller.Installer However when you create an object out of with PowerShell you don't get any of the properties or methods: $object = New-Object -Com WindowsInstaller.Installer $object |

Hashtables and key order

好久不见. 提交于 2019-11-29 01:02:41
Is there a way to keep the order of keys in a hashtable as they were added? Like a push/pop mechanism. Example: $hashtable = @{} $hashtable.Add("Switzerland", "Bern") $hashtable.Add("Spain", "Madrid") $hashtable.Add("Italy", "Rome") $hashtable.Add("Germany", "Berlin") $hashtable I want to retain the order in which I've added the elements to the hashtable. Loïc MICHEL There is no built-in solution in PowerShell V1 / V2. You will want to use the .NET System.Collections.Specialized.OrderedDictionary : $order = New-Object System.Collections.Specialized.OrderedDictionary $order.Add("Switzerland",

How to capture multiple regex matches, from a single line, into the $matches magic variable in Powershell?

妖精的绣舞 提交于 2019-11-29 00:58:30
问题 Let's say I have the string "blah blah F12 blah blah F32 blah blah blah" and I want to match the F12 and F32 , how would I go about capturing both to the Powershell magic variable $matches? If I run the following code in Powershell: $string = "blah blah F12 blah blah F32 blah blah blah" $string -match "F\d\d" The $matches variable only contains F12 I also tried: $string -match "(F\d\d)" This time $matches had two items, but both are F12 I would like $matches to contain both F12 and F32 for

How to get hash table key which has specific value?

梦想的初衷 提交于 2019-11-28 23:28:20
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 denoted as True? Also i would like to know whether hash table is a wise choice for this kind of work.

PowerShell 2.0: Accessing Windows Shares during a Remote Session

倖福魔咒の 提交于 2019-11-28 22:53:37
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, the contents are correctly listed. I've tried various things using credentials, but that doesn't seem

Gracefully stopping in Powershell

不打扰是莪最后的温柔 提交于 2019-11-28 22:45:27
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). FkYkko You could use the method described on here on PoshCode Summary: Set [console]::TreatControlCAsInput = $true then poll for user input using if($Host.UI.RawUI.KeyAvailable -and (3 -eq [int]$Host.UI.RawUI.ReadKey("AllowCtrlC