ConvertTo-JSON an array with a single item
Try without the pipeline: PS C:\> ConvertTo-Json @(‘one’, ‘two’) [ “one”, “two” ] PS C:\> ConvertTo-Json @(‘one’) [ “one” ]
Try without the pipeline: PS C:\> ConvertTo-Json @(‘one’, ‘two’) [ “one”, “two” ] PS C:\> ConvertTo-Json @(‘one’) [ “one” ]
That is because your powershell session on the remote machine does not go to any visible desktop, but to an invisible system desktop. The receiving end of your powershell remote session is a Windows service. The process is started, but nor you nor anyone else can ever see it. And if you think about it, … Read more
Specifying inheritance in the FileSystemAccessRule() constructor fixes this, as demonstrated by the modified code below (notice the two new constuctor parameters inserted between “FullControl” and “Allow”). $Acl = Get-Acl “\\R9N2WRN\Share” $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule(“user”, “FullControl”, “ContainerInherit,ObjectInherit”, “None”, “Allow”) $Acl.SetAccessRule($Ar) Set-Acl “\\R9N2WRN\Share” $Acl According to this topic “when you create a FileSystemAccessRule the way you have, … Read more
In search of this same solution, I found what I needed under a different question in stackoverflow: Powershell-log-off-remote-session. The below one line will return a list of logged on users. query user /server:$SERVER
PowerShell is doing the hard work for us and it loops over the collection internally. I like to call this “implicit foreach”. Assuming the member you specified is present on each object, if the member you specified is a property, you get back its value. If it’s a method, it invokes the method on the … Read more
It’s a trouble with the depth, the default value is 2, can you try : ConvertTo-json @($val) -Depth 5
The other answer does get you the response, but you need an additional step to get the actual body of the response, not just the headers. Here is a snippet: try { $result = Invoke-WebRequest … } catch { $result = $_.Exception.Response.GetResponseStream() $reader = New-Object System.IO.StreamReader($result) $reader.BaseStream.Position = 0 $reader.DiscardBufferedData() $responseBody = $reader.ReadToEnd(); }
$json = @” { “Stuffs”: [ { “Name”: “Darts”, “Type”: “Fun Stuff” }, { “Name”: “Clean Toilet”, “Type”: “Boring Stuff” } ] } “@ $x = $json | ConvertFrom-Json $x.Stuffs[0] # access to Darts $x.Stuffs[1] # access to Clean Toilet $darts = $x.Stuffs | where { $_.Name -eq “Darts” } #Darts
In PowerShell there are terminating and non-terminating errors. The former terminate script execution (if not caught) and can be caught by try..catch, the latter don’t terminate script execution (so there’s nothing to catch). The error you’re receiving is a non-terminating one, so you need to make it a terminating error by appending -ErrorAction Stop to … Read more
LoadWithPartialName has been deprecated. The recommended solution for PowerShell V3 is to use the Add-Type cmdlet e.g.: Add-Type -Path ‘C:\Program Files\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll’ There are multiple different versions and you may want to pick a particular version. 🙂