powershell-2.0

Cannot process argument transformation

梦想与她 提交于 2019-12-05 09:26:49
Inspired by this post , I created the script below DOSCommands.ps1 Function Invoke-DOSCommands { Param( [Parameter(Position=0,Mandatory=$true)] [String]$cmd, [String]$tmpname = $(([string](Get-Random -Minimum 10000 -Maximum 99999999)) + ".cmd"), [switch]$tmpdir = $true) if ($tmpdir) { $cmdpath = $(Join-Path -Path $env:TEMP -ChildPath $tmpname); } else { $cmdpath = ".\" + $tmpname } Write-Debug "tmpfile: " + $cmdpath Write-Debug "cmd: " + $cmd echo $cmd | Out-File -FilePath $cmdpath -Encoding ascii; & cmd.exe /c $cmdpath | Out-Null } Invoke-DOSCommands "Echo ""Hello World""", -tmpdir $false

How to check whether a node exists or not using powershell without getting exception?

萝らか妹 提交于 2019-12-05 06:37:05
I am trying to check whether a particular node exists or not like follows. In my config file there is a node named client ,it may or may not available. If it is not available i have to add it. $xmldata = [xml](Get-Content $webConfig) $xpath="//configuration/system.serviceModel" $FullSearchStr= Select-XML -XML $xmldata -XPath $xpath If ( $FullSearchStr -ne $null) { #Add client node $client = $xmldata.CreateElement('Client') $client.set_InnerXML("$ClientNode") $xmldata.configuration."system.serviceModel".AppendChild($client) $xmldata.Save($webConfig) } The condition i am checking may return

Using PowerShell with .NET 3.5 runtime/libraries

安稳与你 提交于 2019-12-05 04:43:38
Is it possible to run PowerShell 1.0 (or 2.0 CTP) backed by the 3.5 runtime instead of 2.0? We're building a .NET 3.5 solution, and I'd still like to use PowerShell as our scripting engine for scheduled tasks, etc. I don't need LINQ syntax or anything, just the 3.5 libraries and runtime. FOLLOWUP: thank you for the reply about dynamically loading assemblies. But let me clarify my question: is there any way to run PowerShell so that the 3.5 libraries run by default? So that if I enter New-Object System.Xml.XmlDocument , for example, I'm actually getting the 3.5 version by default? Semi-related

Reference command name with dashes

眉间皱痕 提交于 2019-12-05 04:33:43
I've recently discovered that Powershell functions are just named scriptblocks. For example function HelloWorld { Write-Output "Hello world" } $hw = $function:HelloWorld & $hw Will execute the HelloWorld method. However, what I have not been able to figure out, is how to get a reference to a method that has a dash in it's name: function Hello-World { Write-Output "Hello world" } $hw = $function:Hello-World You must provide a value expression on the right-hand side of the '-' operator. At line:1 char:27 + $hw = $function:Hello- <<<< World + CategoryInfo : ParserError: (:) [],

Pass a function (with arguments) as a parameter in PowerShell

て烟熏妆下的殇ゞ 提交于 2019-12-05 02:51:58
I've been successfully passing no-argument functions around in PowerShell using ScriptBlocks. However, I can't get this to work if the function has arguments. Is there a way to do this in PowerShell? (v2 preferably) Function Add([int] $x, [int] $y) { return $x + $y } Function Apply([scriptblock] $s) { write-host ($s.Invoke(1,2)) } Then Apply { Add } writes 0 to the console. Apply does invoke Add , but doesn't pass any arguments in (i.e. uses the default [int] values of 0 and 0) Rob Ok, I found the answer here : I wanted ${function:Add} rather than { Add } in the call to Apply . There is a much

Powershell - How to check if file with desired creation time exists?

随声附和 提交于 2019-12-05 02:45:57
问题 I'm using this script $date = Get-Date Get-Childitem -name | where {$_.CreationTime -eq "$date"} but it doesn't work. All I need is to do a boolean check if there is a file created in specific date. Thank you. 回答1: Get-Date gets the current date and time. If you compare a file's creation date with the current date and time you won't find any files (except you just have created one while retrieving the current date ;) ). $today = (get-date).Date Get-ChildItem | where { $_.CreationTime.Date -eq

Convert seconds to hh:mm:ss,fff format in PowerShell

落花浮王杯 提交于 2019-12-05 00:45:33
I have a string representing a time in seconds and milliseconds. I want to convert it to a string in the format "hh:mm:ss,fff". My solution still has the flaw that hours less than 10 are shown with one decimal instead of two: PS> $secs = "7000.6789" PS> $ts = [timespan]::fromseconds($s) PS> $res = "$($ts.hours):$($ts.minutes):$($ts.seconds),$($ts.milliseconds)" PS> $res PS> 1:56:40,679 What is the right way to achieve this? I'm sure there is a more elegant way with -f and datetime. In PowerShell 4.0 $s = "7000.6789" $ts = [timespan]::fromseconds($s) ("{0:hh\:mm\:ss\,fff}" -f $ts) Output: 01:56

File Output in Powershell without Extension

岁酱吖の 提交于 2019-12-05 00:08:50
Here is what I have so far: Get-ChildItem "C:\Folder" | Foreach-Object {$_.Name} > C:\Folder\File.txt When you open the output from above, File.txt, you see this: file1.txt file2.mpg file3.avi file4.txt How do I get the output so it drops the extension and only shows this: file1 file2 file3 file4 Thanks in advance! EDIT Figured it out with the help of the fellows below me. I ended up using: Get-ChildItem "C:\Folder" | Foreach-Object {$_.BaseName} > C:\Folder\File.txt Get-ChildItem "C:\Folder" | Select BaseName > C:\Folder\File.txt Pass the file name to the GetFileNameWithoutExtension method to

How to change the value of XML Element attribute using PowerShell?

最后都变了- 提交于 2019-12-04 23:44:17
I am trying to access and change the particular attribute from XML tag XML: <office> <staff branch="Hanover" Type="sales"> <employee> <Name>Tobias Weltner</Name> <function>management</function> <age>39</age> </employee> <employee> <Name>Cofi Heidecke</Name> <function>security</function> <age>4</age> </employee> </staff> <staff branch="London" Type="Technology"> <employee> <Name>XXXX</Name> <function>gement</function> <age>39</age> From the above example I want to print branch attribute and then want to change it with one value such as New York in all the whole XML and using below code to do

How to execute a powershell script available in remote machine?

安稳与你 提交于 2019-12-04 22:22:20
I was trying to execute a script in remote computer. I did " Enable-PSremoting " in the remote machine. I placed a script hello.ps1 in remote machine. [My client machine is Windows XP and remote computer is Windows 2003 ] Then from my client computer i was trying to execute the script. invoke-command -computer $MachineName -filepath "C:\hello.ps1" I got the following error. Invoke-Command : Cannot find path 'C:\hello.ps1' because it does not exist. I think it tries to find script from client machine. If i try to run invoke-command -computer $MachineName -command { C:\hello.ps1 } , It executes