powershell-4.0

How do I get PowerShell 4 cmdlets such as Test-NetConnection to work on Windows 7?

一笑奈何 提交于 2019-12-03 05:06:08
The situation. On a Windows 7 SP1 machine, I have updated with Windows6.1-KB2819745-x64-MultiPkg.msu. Furthermore, in PowerShell $PSVersionTable now reports ‘PSVersion 4.0’. At present, my conclusion is that many PowerShell 4 cmdlets such Test-NetConnection, will only work on Windows 8.1. However, I was wondering if there was a work-around whereby I could import PowerShell 4 modules on my Windows 7 machine. Knuckle-Dragger You cannot, they rely on underlying features of the newer OS (8.0 or 8.1) and cannot be ported back to W7. The alternative is to write your own functions / modules to

How to change tab width when converting to JSON in Powershell

守給你的承諾、 提交于 2019-12-03 02:40:59
I am creating a JSON in Powershell and I want to set a custom tab width when building it (instead of the default 4 white spaces I want to set only 2 white spaces). I am doing this because: the actual JSON (not the one presented in the below sample) is pretty big (100k+ rows) and if not archieved, it's size is pretty big; If I reduce the tab width the size reduction is notable. the actual JSON has a depth of 5+ nodes ! I cannot use -Compress since the JSON needs to be human readable Yes, I agree, if archived, it's size is dramatically reduced but I need it also unarchived. Sample code: $object

Add Element to XML

为君一笑 提交于 2019-12-02 13:40:36
I want to add a element (connector) to a existing XML, this was succesfull but I need to remove the xmlns= and want to add an value to it. The connector blaat is added with my code. The XML: <?xml version="1.0"?> <configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd"> <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq:core "> <connectors> <!-- Connector used to be announced through

Equivalent of *nix fold in PowerShell

孤街醉人 提交于 2019-12-02 05:15:30
问题 Today I had a few hundred items (IDs from SQL query) and needed to paste them into another query to be readable by an analyst. I needed *nix fold command. I wanted to take the 300 lines and reformat them as multiple numbers per line seperated by a space. I would have used fold -w 100 -s . Similar tools on *nix include fmt and par . On Windows is there an easy way to do this in PowerShell? I expected one of the *-Format commandlets to do it, but I couldn't find it. I'm using PowerShell v4. See

Mysticism: Invoke-WebRequest working only via ISE

☆樱花仙子☆ 提交于 2019-12-02 00:23:01
问题 I have killed 3 hours today and don't understand why? I have easy script: $user = 'icm' $pass = 'icm' $pair = "$($user):$($pass)" $url = 'http://####:15672/api/queues/%2f/ICM.Payments.Host.1' $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) $basicAuthValue = "Basic $encodedCreds" $headers = @{ Authorization = $basicAuthValue } $request = Invoke-WebRequest -Uri $url -Headers $headers -ContentType "application/json" $messages = ($request.Content |

Can you mimic PowerShell v3 once you have installed v4?

你。 提交于 2019-12-01 20:43:09
问题 I want to emulate PowerShell 3.0 From PowerShell v3 or v4 you can emulate v2 with this command: %windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -version 2 $Host reveals: Version: 2.0 However, in PowerShell 4 %windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -version 3 $Host now reports: Version: 4.0 (I was hoping for 3:0) Question: Is there any way of emulating PowerShell 3.0 from 4.0? 回答1: No, it's not possible. V4 is backwards compatible with V3, so any script written

Group-Object diffencies with or without code block

故事扮演 提交于 2019-12-01 18:49:27
The code below produce 2 "identical" Hashtables, however on the one that was grouped using a code block I can't get items from the key. $HashTableWithoutBlock = Get-WmiObject Win32_Service | Group-Object State -AsHashTable $HashTableWithBlock = Get-WmiObject Win32_Service | Group-Object {$_.State} -AsHashTable Write-Host "Search result for HashTable without using code block : " -NoNewline if($HashTableWithoutBlock["Stopped"] -eq $null) { Write-Host "Failed" } else { Write-Host "Success" } Write-Host "Search result for HashTable with code block : " -NoNewline if($HashTableWithBlock["Stopped"]

Function does not get binding when used in Module

。_饼干妹妹 提交于 2019-12-01 09:24:29
I'm trying to put this function : function Test-Any { [CmdletBinding()] param($EvaluateCondition, [Parameter(ValueFromPipeline = $true)] $ObjectToTest) begin { $any = $false } process { if (-not $any -and (& $EvaluateCondition $ObjectToTest)) { $any = $true } } end { $any } } into a module. I just created a new module, the my-scripts.psm1 file, which contains just the above function and import it with Import-Module <absolute path> . The problem is that if I use the function from the module 1..4 | Test-Any { $_ -gt 3 } returns false, because $_ is not set to the value from the pipe. If I define

Function does not get binding when used in Module

左心房为你撑大大i 提交于 2019-12-01 05:43:44
问题 I'm trying to put this function: function Test-Any { [CmdletBinding()] param($EvaluateCondition, [Parameter(ValueFromPipeline = $true)] $ObjectToTest) begin { $any = $false } process { if (-not $any -and (& $EvaluateCondition $ObjectToTest)) { $any = $true } } end { $any } } into a module. I just created a new module, the my-scripts.psm1 file, which contains just the above function and import it with Import-Module <absolute path> . The problem is that if I use the function from the module 1.

Extract value from JSON

两盒软妹~` 提交于 2019-11-30 21:56:21
I am trying to use PowerShell to extract value from JSON object, I have the following JSON: { "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", "contentVersion": "1.0.0.0", "parameters": { "clusterName": { "value": "hbasehd35" }, "location": { "value": "East US 2" }, "clusterType": { "value": "hbase" }, "clusterVersion": { "value": "3.5" }, "clusterWorkerNodeCount": { "value": 5 }, "subnetName": { "value": "hbase-subnet" }, "headNodeSize": { "value": "Standard_D12_v2" }, "workerNodeSize": { "value": "Standard_D12_v2" }, "zookeeperSize": { "value":