powershell

Powershell - Count number of carriage returns line feed in .txt file

怎甘沉沦 提交于 2021-02-04 21:07:23
问题 I have a large text file (output from SQL db) and I need to determine the row count. However, since the source SQL data itself contains carriage returns \r and line feeds \n (NEVER appearing together), the data for some rows spans multiple lines in the output .txt file. The Powershell I'm using below gives me the file line count which is greater than the actual SQL row count. So I need to modify the script to ignore the additional lines - one way of doing it might be just counting the number

Get-hotfix on multiple computers and exporting to CSV

你离开我真会死。 提交于 2021-02-04 21:05:34
问题 How do I properly use $_ in out-file? Here's my code: get-content computers.txt | Where {$_ -AND (Test-Connection $_ -Quiet)} | foreach { Get-Hotfix -computername $_ } | Select CSName,Description,HotFixID,InstalledBy,InstalledOn | convertto-csv | out-file "C:\$_.csv" I'm trying to execute a get-hotfix for all the computers listed in the text file then I want them to be exported to CSV with the computer name as the filename. 回答1: You need one pipeline to process the computers.txt files, and a

Create a Single-Element Json Array Object Using PowerShell

倖福魔咒の 提交于 2021-02-04 20:38:36
问题 I have a powershell 5.1 script that I've created that imports a csv file, manipulates some of the data, then converts the object of data to json format using the ConvertTo-Json cmdlet at the end of the script. The problem that I'm running into; in one of my fields I need to create a single array for the object property with brackets. So I need the object to be like: "PersonGroups":[ { "Name":"test Name", "Id": 3433 } ] Here is the call to the function: $_.PersonGroups = Set-DataHash

Create a Single-Element Json Array Object Using PowerShell

隐身守侯 提交于 2021-02-04 20:37:39
问题 I have a powershell 5.1 script that I've created that imports a csv file, manipulates some of the data, then converts the object of data to json format using the ConvertTo-Json cmdlet at the end of the script. The problem that I'm running into; in one of my fields I need to create a single array for the object property with brackets. So I need the object to be like: "PersonGroups":[ { "Name":"test Name", "Id": 3433 } ] Here is the call to the function: $_.PersonGroups = Set-DataHash

Powershell - How to UpperCase a string found with a Regex [duplicate]

痴心易碎 提交于 2021-02-04 19:50:31
问题 This question already has answers here : Lambda Expression in Powershell (3 answers) Closed last year . I am writing a powershell script to parse the HTM file. I need to find all the links file in the file and then uppercase the filepath, filename and extention. (could be 30 or 40 links in any file). The part I'm having trouble with is the 2nd part of the -replace staement below (the 'XXXX' part). The regex WILL find the strings I'm looking for but I can't figure out how to 'replace' that

Powershell - How to UpperCase a string found with a Regex [duplicate]

泪湿孤枕 提交于 2021-02-04 19:50:06
问题 This question already has answers here : Lambda Expression in Powershell (3 answers) Closed last year . I am writing a powershell script to parse the HTM file. I need to find all the links file in the file and then uppercase the filepath, filename and extention. (could be 30 or 40 links in any file). The part I'm having trouble with is the 2nd part of the -replace staement below (the 'XXXX' part). The regex WILL find the strings I'm looking for but I can't figure out how to 'replace' that

Understanding NewtonSoft in PowerShell

强颜欢笑 提交于 2021-02-04 19:45:45
问题 I making a foray into the world of JSON parsing and NewtonSoft and I'm confused, to say the least. Take the below PowerShell script: $json = @" { "Array1": [ "I am string 1 from array1", "I am string 2 from array1" ], "Array2": [ { "Array2Object1Str1": "Object in list, string 1", "Array2Object1Str2": "Object in list, string 2" } ] } "@ #The newtonSoft way $nsObj = [Newtonsoft.Json.JsonConvert]::DeserializeObject($json, [Newtonsoft.Json.Linq.JObject]) $nsObj.GetType().fullname #Type =

How to run Start-Process in Powershell using user credentials?

佐手、 提交于 2021-02-04 19:10:39
问题 I've got a Windows service (Jenkins) that runs a script which needs to run a command as a specific user. I tried to do this but it doesn't work: $secpasswd = ConvertTo-SecureString "myPassword" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential("DOMAIN\myUsername", $secpasswd) $Arguments = @() $Arguments += "-Command" $Arguments += "pwd" $Arguments += ">" $Arguments += "output.txt" Start-Process powershell.exe -ArgumentList $Arguments -Credential $mycreds

How to run Start-Process in Powershell using user credentials?

人走茶凉 提交于 2021-02-04 19:09:37
问题 I've got a Windows service (Jenkins) that runs a script which needs to run a command as a specific user. I tried to do this but it doesn't work: $secpasswd = ConvertTo-SecureString "myPassword" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential("DOMAIN\myUsername", $secpasswd) $Arguments = @() $Arguments += "-Command" $Arguments += "pwd" $Arguments += ">" $Arguments += "output.txt" Start-Process powershell.exe -ArgumentList $Arguments -Credential $mycreds

How to run Start-Process in Powershell using user credentials?

前提是你 提交于 2021-02-04 19:08:44
问题 I've got a Windows service (Jenkins) that runs a script which needs to run a command as a specific user. I tried to do this but it doesn't work: $secpasswd = ConvertTo-SecureString "myPassword" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential("DOMAIN\myUsername", $secpasswd) $Arguments = @() $Arguments += "-Command" $Arguments += "pwd" $Arguments += ">" $Arguments += "output.txt" Start-Process powershell.exe -ArgumentList $Arguments -Credential $mycreds