powershell-5.0

PowerShell $null is not null any more when calling into C# code

旧城冷巷雨未停 提交于 2019-12-19 21:11:32
问题 In PowerShell one can define C# code and execute it. Passing in $null into the following simplest function shows that not null gets passed into the function Add-Type -TypeDefinition @" public static class foo { public static void fooo(string a) { if(a!=null) { System.Console.WriteLine("Not Null. Is '" + a + "'"); } } } "@ -Language CSharp Calling it as follows leads to the output Not Null. Is ''. This shows that $null was not null in C#. Methods like 'IsNullOrEmpty' or 'IsNullOrWhiteSpace'

PowerShell5. Modify ascii text file string with line number string is on. Switch and .NET framework or cmdlets & the pipeline? Which is faster?

♀尐吖头ヾ 提交于 2019-12-19 10:15:25
问题 How to modify a string (LINE2 "line number LINE2 is on") in a windows ascii text file using search strings that are easy to read and easy to add/modify/delete using PowerShell 5. This script will parse a 2500 line file, find 139 instances of the strings, replace them and overwrite the original in less than 165ms on average depending on which method you use. Which method is faster? Which method is easier to add/modify/delete the strings? Search for strings "AROUND LINE {1-9999}" and "LINE2 {1

New-Item changes function return value

穿精又带淫゛_ 提交于 2019-12-19 09:48:08
问题 I want to write a function that creates a folder (if it doesn't exist) with specified name inside specified folder. It's turned out that depending on calling New-Item function return different values. And I can't figure out how it's related to New-Item actually. $folderPath = "C:\tmp" function CreateFolder([string] $name, [string] $parentFolder) { $path = "$parentFolder\$name" if(!(Test-Path $path)) { New-Item -path $parentFolder -name $name -itemtype Directory } return $path } $FOLDER_NAME =

Piping ListView items in separate runspace

烈酒焚心 提交于 2019-12-17 21:27:28
问题 I have a form written in PowerShell (as it uses PS to run commands over hundreds of servers) but this bit had me stumped for awhile: $listview.Invoke([System.Action[String]]{ Param ( [String]$data ) write-warning "1" $LVitem = ($LVresults.Items | Where { $_.Name -eq "Test Account" }) write-warning "2" $LVitem.Tag.Output += $data + "`n" }, "Testing") It's in a separate runspace that runs an Invoke-Command to a specific server and pipes the output to this code block. Now if I run the Where

Iterating through a JSON file PowerShell

点点圈 提交于 2019-12-17 11:27:12
问题 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":

How to set PITR for all databases in a Azure Sql Database Elastic Pool?

▼魔方 西西 提交于 2019-12-13 04:34:41
问题 I think I'm running into version issues because the cmdlet I want seems to only be available in AzureRM.Sql module version 4.7.0-preview . I want to set the PITR retention policy for many databases in an elastic pool to 35 days. By default my vCore pool has a retention policy of 7 days which is not enough. I have hundreds of databases so need to set them all with PowerShell. If I get the list of databases to update with Get-AzureRmSqlElasticPoolDatabase and then try to run Set

Simple PS script but dirty output

倾然丶 夕夏残阳落幕 提交于 2019-12-13 03:39:40
问题 I'm having an issue with a script I'm writing. The code works but it's not spitting out the info how I'd like to see it. Basically, it spits it out with a few carriage returns between the final two commands. I also tried to dump the Get-ADUser command into an array and then add the data from the $Pass into said array but that failed miserably and I could never get that to work; every which way I tried to write and execute it, would come up with errors. Am I on the right track or is there a

PowerShell-V5 Invoke-Webrequest adding 2 headers authorization header and accept accept header

佐手、 提交于 2019-12-12 21:07:53
问题 I am trying create a script that will automate the upgrade of NSX using powershell and invoke-webrequest to utilize the API calls of the NSX Manager. I am doen with the script but some part of the script i need to check and match on some data in the respond. as a matter of fact the NSX API response will 90% be in xml format but some times it can be in json. If the response came in json format my matching will not work and also some parts of the script will not work as it is expecting xml and

DSC problems with Credentials and build 10586

有些话、适合烂在心里 提交于 2019-12-12 18:06:44
问题 The latest windows 10 build pushed out an updated version of powershell (10586). In addition to the change required for the certificate documented at https://dscottraynsford.wordpress.com/2015/11/15/windows-10-build-10586-powershell-problems/ i seem to have an additional problem, while trying to apply the configuration: WarningMessage An error occured while applying the partial configuration [PartialConfiguration]ExternalIntegrationConfiguration. The error message is : Decryption failed..

Powershell - Import local CSV

时间秒杀一切 提交于 2019-12-12 05:32:16
问题 I have a powershell script that imports a CSV file using this command: $empCsv = Import-Csv "addEmp.csv" I have the csv sitting in the same directory as the script but I'm getting a FileNotFoundException . If I use the command with a path to the CSV like this: $empCsv = Import-Csv .\Desktop\createUser\addEmp.csv It works fine. I want to be able to give this script to someone to use and let them run the script from any location and be able to import the .csv that's sitting in the same