powershell-2.0

How to use add-type with -path and also -language csharpversion3?

强颜欢笑 提交于 2019-12-21 19:52:32
问题 I've been using add-type in Powershell to dynamically compile in some C# classes I want to use. Works great except it's 2.0 only. I just discovered the -language csharpversion3 option, but it does not work with -path . How can I work around this? [Edit: removed bit about ReadAllText - I was mistaken.] 回答1: Here's the workaround: read the file as text. $script = [io.file]::readalltext($scriptpath) add-type $script -lang csharpversion3 I might as well paste in the rest and make this answer

Setting a variable in batch using powershell

こ雲淡風輕ζ 提交于 2019-12-21 19:49:58
问题 I have been racking my brain trying to figure this out. this code @echo off powershell $Yesterday = (get-date((get-date).addDays(-1)) -format yyyyMMdd) echo %Yesterday% ::neither one of these echo anything @echo off powershell Set-Variable -Name "Yesterday" -Value (get-date((get-date).addDays(-1)) -format yyyyMMdd) echo %Yesterday% should both return a response with yesterdays date (formatted as yyyMMdd), however, they do not. Using powershell, the following code does indeed work and return

Powershell script to Upload log file from local system to http URL

依然范特西╮ 提交于 2019-12-21 17:27:26
问题 How could i upload a log file from my local system to a webpage (http://abc..) using powershell script? Thanks in advance 回答1: If you are using HTTP, try something like this: $sourceFilePath = "c:\MyLocalFolder\LocalLogFileName.log" $siteAddress = "http://192.168.15.12/DestinationFolder" $urlDest = "{0}/{1}" -f ($siteAddress, "DestinationLogFileName.log"; $webClient = New-Object System.Net.WebClient; $webClient.Credentials = New-Object System.Net.NetworkCredential("MyUserName", "MyPassword");

Merge multiple XML files into one using PowerShell 2.0?

家住魔仙堡 提交于 2019-12-21 13:10:46
问题 I have a directory of very large XML files with a structure as this: file1.xml: <root> <EmployeeInfo attr="one" /> <EmployeeInfo attr="two" /> <EmployeeInfo attr="three" /> </root> file2.xml: <root> <EmployeeInfo attr="four" /> <EmployeeInfo attr="five" /> <EmployeeInfo attr="six" /> </root> Now I am looking for a simple way to merge these files (*.xml) files into one output file: <root> <EmployeeInfo attr="one" /> <EmployeeInfo attr="two" /> <EmployeeInfo attr="three" /> <EmployeeInfo attr=

What objects are suitable for Add-Member?

心已入冬 提交于 2019-12-21 12:41:25
问题 Documentation states: Adds a user-defined custom member to an instance of a Windows PowerShell object. What "Windows PowerShell object" stands for? This works fine: $obj = new-object system.object $obj | add-member -membertype noteproperty -name Name -value "OK" $obj.name But this does not: $obj = @{} Actually, I am trying to add property to $error[0]. 回答1: PowerShell has what's called a PSObject that is a wrapper around any .NET object (or it can be a totally custom object) and when you call

powershell - how to check if transcript is running?

荒凉一梦 提交于 2019-12-21 07:33:58
问题 I get this message everytime my script doesn't end properly and stop-transcript is not executed: Start-Transcript : Transcription has already been started. Use the stop-transcr ipt command to stop transcription. At C:\ps\003desifrovanie.ps1:4 char:17 + start-transcript <<<< -path c:\_LOG\sfrbdesifrovanie.log -append + CategoryInfo : NotSpecified: (:) [Start-Transcript], InvalidOpe rationException + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Power Shell.Commands

My PowerShell exceptions aren't being caught

六月ゝ 毕业季﹏ 提交于 2019-12-21 05:32:16
问题 Powershell v2: try { Remove-Item C:\hiberfil.sys -ErrorAction Stop } catch [System.IO.IOException] { "problem" } catch [System.Exception] { "other" } I'm using the hibernation file as an example, of course. There's actually another file that I expect I may not have permission to delete sometimes, and I want to catch this exceptional situation. Output: output and yet $error[0] | fl * -Force outputs System.IO.IOException: Not Enough permission to perform operation. Problem: I don't see why I'm

powershell 2.0 command line redirection

跟風遠走 提交于 2019-12-21 04:24:09
问题 I'm looking for an explanation of the following discrepancy: Given the following powershell script foo.ps1 : write-host "normal" write-error "error" write-host "yay" Running it with C:\>powershell .\foo.ps1 > out.txt 2>&1 Produces: normal C:\foo.ps1 : error At line:1 char:10 + .\foo.ps1 <<<< + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,foo.ps1 Write-Host : The OS handle's position is not what

Iterating through key names from a PSCustomObject

瘦欲@ 提交于 2019-12-21 03:37:21
问题 I am writing a script for my site that utilizes a JSON configuration file. The JSON is similar to the following: "Groups": { "GroupOne": { "NamingFilter": { "Not":"<SITE>-MJ*", "Has":"*WM,*WT" } }, "GroupTwo": { "NamingFilter": { "Has":"<SITE>-MJ*, *WC,*WL" } }, "GroupThree": { "NamingFilter": { "Not":"<SITE>-MJ*", "Has":"*WI" } } } To convert the object to something PowerShell can read, I use ConvertFrom-Json which converts it to type PSCustomObject . I am at a point where I have to iterate

How to timeout PowerShell function call

走远了吗. 提交于 2019-12-20 19:32:45
问题 I wrote a little powershell function that executes Get-EventLog against remote servers. On some servers this seems to just hang and never times out. Can I timeout a powershell function call? I see how to do this against a different process, but i want to do this for a power shell function. thanks ####################### function Get-Alert4 { param($computer) $ret = Get-EventLog application -after (get-date).addHours(-2) -computer $computer | select-string -inputobject{$_.message} -pattern