powershell-5.0

Making a PowerShell POST request if a body param starts with '@'

时光总嘲笑我的痴心妄想 提交于 2019-11-27 20:03:04
I want to make a POST request in PowerShell. Following is the body details in Postman. { "@type":"login", "username":"xxx@gmail.com", "password":"yyy" } How do I pass this in PowerShell? You should be able to do the following: $params = @{"@type"="login"; "username"="xxx@gmail.com"; "password"="yyy"; } Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body $params This will send the post as the body. However - if you want to post this as a Json you might want to be explicit. To post this as a JSON you can specify the ContentType and convert the body to Json by using Invoke

Get error while running Enable-Migrations

懵懂的女人 提交于 2019-11-27 15:10:22
问题 I got below error while running Enable-Migrations on my ASP.NET MVC5 project (Powershell v5 & Visual Studio 2015). I have tried to uninstall and re-install EntityFramework (v6.1.3) but no lucky. Does anyone know how to solve it? Type name 'Microsoft.VisualStudio.Shell.Package' is ambiguous, it could be 'Microsoft.VisualStudio.Shell.Package, Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or 'Microsoft.VisualStudio.Shell.Package, Microsoft

Iterating through a JSON file PowerShell

倾然丶 夕夏残阳落幕 提交于 2019-11-27 14:19:28
I am trying to loop through the below JSON file in PowerShell. Without specifically naming the top tags (e.g. 17443 and 17444), as I do not know them in advance I cannot find a way to loop through the data. I want to output tags 3, 4 and 5 (title, firstname, surname) for all the records. How would I accomplish that? { "17443":{ "sid":"17443", "nid":"7728", "submitted":"1436175407", "data":{ "3":{ "value":[ "Mr" ] }, "4":{ "value":[ "Jack" ] }, "5":{ "value":[ "Cawles" ] } }, "17444":{ "sid":"17444", "nid":"7728", "submitted":"1436891400", "data":{ "3":{ "value":[ "Miss" ] }, "4":{ "value":[

Block of code prevents script from running, but runs interactively

南楼画角 提交于 2019-11-27 07:57:54
问题 I have a script that is run as a scheduled task which fails with an unexpected token error on on the line where $As is defined. If I remove the code, the script runs properly. If I paste the whole script (including the problematic section) into a PowerShell window everything runs as expected. I'm assuming this is a simple gotcha that I've just not encountered, but I cannot figure out what the problem is with it, more experienced eyes would be appreciated. This is being run on Server 2012R2,

PowerShell type accelerators: PSObject vs PSCustomObject

為{幸葍}努か 提交于 2019-11-27 07:37:13
问题 In PowerShell v3.0 PSCustomObject was introduced. It's like PSObject , but better. Among other improvements (e.g. property order being preserved), creating object from hashtable is simplified: [PSCustomObject]@{one=1; two=2;} Now it seems obvious that this statement: [System.Management.Automation.PSCustomObject]@{one=1; two=2;} would work the same way, because PSCustomObject is an "alias" for full namespace + class name. Instead I get an error: Cannot convert the "System.Collections.Hashtable

Set-Content appends a newline (line break, CRLF) at the end of my file

狂风中的少年 提交于 2019-11-27 04:38:40
My original config file (web1.config) has no extra line and when viewed in notepad (showing all characters) looks as: <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.6" /> <httpRuntime targetFramework="4.6" /> </system.web> <appSettings> <add key="myConnectionString" value="server=localhost;database=myDb;uid=myUser;password=myPass;" /> </appSettings> </configuration> Now, I need to apply the script to change my database name to something else which looks like: Move-Item "web1.config" "webtemp.config" Get-Content "webtemp.config" | ForEach-Object

how to export a class in powershell v5 module

爷,独闯天下 提交于 2019-11-27 01:31:33
问题 I've got a module setup to be like a library for a few other scripts. I can't figure out how to get a class declaration into the script scope calling Import-Module . I tried to arrange Export-Module with a -class argument, like the -function , but there isn't a -class available. Do I just have to declare the class in every script? The setup: holidays.psm1 in ~\documents\windows\powershell\modules\holidays\ active script calls import-module holidays there is another function in holidays.psm1

PowerShell 5 and classes - Cannot convert the “X” value of type “X” to type “X”

那年仲夏 提交于 2019-11-26 23:38:40
问题 I'm trying to use PowerShell's classes that is very handy way of grouping related data together and facing quite tedious to deal with behavior. The simplified scenario: one PS script that defines class and another script that uses that class. Common.ps1 class X { [string] $A } Script1.ps1 . $PSScriptRoot\Common.ps1 [X] $v = New-Object X Everything is good - you can run Script1.ps1 arbitrary amount of times with no issues - until you make any change in Common.ps1 . You will face the following

Making a PowerShell POST request if a body param starts with '@'

核能气质少年 提交于 2019-11-26 22:47:37
问题 I want to make a POST request in PowerShell. Following is the body details in Postman. { "@type":"login", "username":"xxx@gmail.com", "password":"yyy" } How do I pass this in PowerShell? 回答1: You should be able to do the following: $params = @{"@type"="login"; "username"="xxx@gmail.com"; "password"="yyy"; } Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body $params This will send the post as the body. However - if you want to post this as a Json you might want to be explicit

Write-Host vs Write-Information in PowerShell 5

隐身守侯 提交于 2019-11-26 15:46:37
问题 It is well known that Write-Host is evil. In PowerShell 5 , Write-Information is added and is considered to replace Write-Host . But, really, which is better? Write-Host is evil for it does not use pipeline, so the input message can't get reused. But, what Write-Host do is just to show something in the console right? In what case shall we reuse the input? Anyway, if we really want to reuse the input, why not just write something like this: $foo = "Some message to be reused like saving to a