powershell-2.0

Redirecting standard input\output in Windows PowerShell

。_饼干妹妹 提交于 2019-11-26 12:42:07
问题 What is the required syntax to redirect standard input/output on Windows PowerShell? On Unix, we use: $./program <input.txt >output.txt How do I execute the same task in PowerShell? 回答1: You can't hook a file directly to stdin, but you can still access stdin. Get-Content input.txt | ./program > output.txt 回答2: For output redirection you can use: command > filename Redirect command output to a file (overwrite) command >> filename APPEND into a file command 2> filename Redirect Errors Input

Redirection of standard and error output appending to the same log file

倾然丶 夕夏残阳落幕 提交于 2019-11-26 12:39:16
问题 I need to collect the standard output and error log from several processes into one single log file. So every output must append to this log file. I want to call all the jobs with lines like this: $p=start-process myjob.bat -redirectstandardoutput $logfile -redirecterroroutput $logfile -wait Where do I have to put the information to append? 回答1: In order to append to a file you'll need to use a slightly different approach. You can still redirect an individual process' standard error and

Web.Config transforms outside of Microsoft MSBuild?

时光怂恿深爱的人放手 提交于 2019-11-26 11:57:32
问题 Is it possible to use Microsoft\'s XML document transform, for preparing web.configs, outside of MSBuild? I would like to use PowerShell to do these transform without having to run this through the MSBuild engine. If Microsoft had used standard XSLT it would be easy to do in PowerShell. From what I can tell I have to use their C:\\Program Files (x86)\\MSBuild\\Microsoft\\VisualStudio\\v10.0\\Web\\Microsoft.Web.Publishing.Tasks.dll which requires a build engine. Thanks 回答1: I created a small

Can&#39;t install nuget package because of “Failed to initialize the PowerShell host”

女生的网名这么多〃 提交于 2019-11-26 11:54:36
问题 All of a sudden, I am getting this error when upgrading Nuget packages. None of the fixes that I have come across work. I am using Visual Studio 2013. \'Newtonsoft.Json 6.0.3\' already installed. Adding \'Newtonsoft.Json 6.0.3\' to Tournaments.Notifications. Successfully added \'Newtonsoft.Json 6.0.3\' to Tournaments.Notifications. Executing script file \'F:\\My Webs\\BasketballTournaments\\MainBranch\\packages\\Newtonsoft.Json.6.0.3\\tools\\install.ps1\'. Failed to initialize the PowerShell

Best way to return values from a function that writes to STDOUT

橙三吉。 提交于 2019-11-26 11:38:30
问题 I have some helper functions that write to STDOUT for logging purposes. Some of these functions return a value to the caller, but the entire output from the function is returned. How can I have my functions write to STDOUT and return a value to the caller without the return value being polluted with all the STDOUT emitted during the function call? I\'m looking for some kind of design pattern or best practise. Consider this script: Function a { Write-Output \"In Function a\" $a = 4 return $a }

Escaping quotes and double quotes

主宰稳场 提交于 2019-11-26 11:00:09
问题 How do I properly escape the quotes in the -param value in the following command line? $cmd=\"\\\\server\\toto.exe -batch=B -param=\"sort1;parmtxt=\'Security ID=1234\'\"\" Invoke-Expression $cmd This of course fails. I tried to escape the quotes (single and double) using the escape character ` and did various combination, but nothing is working. 回答1: Escaping parameters like that is usually source of frustration and feels a lot like a time wasted. I see you're on v2 so I would suggest using a

How to get an MD5 checksum in PowerShell

扶醉桌前 提交于 2019-11-26 09:05:24
问题 I would like to calculate an MD5 checksum of some content. How do I do this in PowerShell? 回答1: If the content is a string: $someString = "Hello, World!" $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider $utf8 = New-Object -TypeName System.Text.UTF8Encoding $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($someString))) If the content is a file: $someFilePath = "C:\foo.txt" $md5 = New-Object -TypeName System.Security.Cryptography

Recursive file search using PowerShell

天涯浪子 提交于 2019-11-26 08:47:37
问题 I am searching for a file in all the folders. Copyforbuild.bat is available in many places, and I would like to search recursively. $File = \"V:\\Myfolder\\**\\*.CopyForbuild.bat\" How can I do it in PowerShell? 回答1: Use the Get-ChildItem cmdlet with the -Recurse switch: Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force 回答2: I use this to find files and then have PowerShell display the entire path of the results: dir -Path C:\FolderName

How to get disk capacity and free space of remote computer

大城市里の小女人 提交于 2019-11-26 06:21:54
问题 I have this one-liner: get-WmiObject win32_logicaldisk -Computername remotecomputer and the output is this: DeviceID : A: DriveType : 2 ProviderName : FreeSpace : Size : VolumeName : DeviceID : C: DriveType : 3 ProviderName : FreeSpace : 20116508672 Size : 42842714112 VolumeName : DeviceID : D: DriveType : 5 ProviderName : FreeSpace : Size : VolumeName : How do I get Freespace and Size of DeviceID C: ? I need to extract just these two values with no other informations. I have tried it with

How to perform keystroke inside powershell?

雨燕双飞 提交于 2019-11-26 06:06:44
问题 I have ps1 script to grab some information from the vmware cluster environment. In some place of ps1 script requires the ENTER button keystroke. So, How to do that ? -Thanks 回答1: If I understand correctly, you want PowerShell to send the ENTER keystroke to some interactive application? $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate('title of the application window') Sleep 1 $wshell.SendKeys('~') If that interactive application is a PowerShell script, just use whatever is