powershell

PowerShell function arguments: Can the first one be optional first?

I have an advanced function in PowerShell, which roughly looks like this: function Foo { [CmdletBinding] param ( [int] $a = 42, [int] $b ) } The idea is that it can be run with either two, one or no arguments. However, the first argument to become optional is the first one. So the following scenarios are po...

Renaming files with an index in the bracket

I'd like to write a short powershell script for renaming files like: abc(1), abc(2), .., abc(10), .., abc(123), .. to abc(001), abc(002), .., abc(010), .., abc(123), .. Any idea? :) ...

Batch file assign returned values from a command into a variable (from powershell)

i am refering to this question ASSIGN win XP commandline output to variable i am trying to use it on a powershell code segment so i typed powershell date (get-date).AddDays(-1) -format yyyyMMdd and confirm it returns like 20100601 but then if i tried to for /f "tokens=*" %a in ('powershell date get-date -format yyyyMMdd ') do s...

Powershell script to create scheduled tasks from csv file

I would like to use Powershell to create a couple of scheduled tasks, on a server. I have created the file from existing schedule's I have loaded up the csv file, piped it to a select, and retreived all the info that I require from the csv file. However I am not sure on how to pass these results on to a external non powershell command...

Smart image search via Powershell

I am interested in file searching by custom properties. For example, I want to find all JPEG-images with certain dimensions. Something looks like Get-ChildItem -Path C:\ -Filter *.jpg -Recursive | where-object { $_.Dimension -eq '1024x768' } I suspect it's about using of System.Drawing. How it can be done? Thanks in advance ...

How to use POWERSHELL to set MimeTypes in an IIS6 website?

I want to be able to replicate this adsutil.vbs behaviour in powershell: cscript adsutil.vbs set W3SVC/$(ProjectWebSiteIdentifier)/MimeMap ".pdf,application/pdf" and I've gotten as far as getting the website object: $website = gwmi -namespace "root\MicrosoftIISv2" -class "IISWebServerSetting" -filter "ServerComment like '%$name%'" i...

PowerShell function won't return object

I have a simple function that creates a generic List: function test() { $genericType = [Type] "System.Collections.Generic.List``1" [type[]] $typedParameters = ,"System.String" $closedType = $genericType.MakeGenericType($typedParameters) [Activator]::CreateInstance($closedType) } $a = test The problem is that $a is alw...

Indirect load of type fails in PowerShell

When invoking [System.Configuration.ConfigurationManager]::GetSection("MySection") from within a PowerShell prompt, it throws an exception because the assembly containing the type represented by "MySection" in the app config is unable to be loaded. However, I have previously loaded the assembly containing that type, and I am even abl...

Opening console applications in powershell

Hi, I'm currently developing a win32 console application, and wondering if there is any way to make visual studio open it in powershell instead of cmd.exe when I'm debugging it. All I really want is a better shell, where I can copy/paste etc. without clicking. Thanks ...

How can I do a screen capture in Windows PowerShell?

How can I capture the screen in Windows PowerShell? I need to be able to save the screen to disk. ...

Powershell Regex help in extracting text between strings

i Have an arguments like the one below which i pass to powershell script -arg1 -abc -def -arg2 -ghi -jkl -arg3 -123 -234 Now i need to extract three strings without any whitespace string 1: "-abc -def" string 2: "-ghi -jkl" string 3: "-123 -234" i figured this expression could do it. But this doesnt seem to work. $args -match '...

Merge multiple XML files into one using PowerShell 2.0?

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...

Powershell/GetEnumerator

I have a simple question: Why the program below does not write the whole array $lines? It is looping forever. I am really confused.. function Get-Current ($enumerator) { $line = $enumerator.Current return $line } $lines = @('a', 'b', 'c') $enumerator = $lines.GetEnumerator() $enumerator.Reset() while ($enumerator.MoveNext()) ...

Powershell 2 remove single binding iis 7

I am trying to remove one site binding. I am using powershell 2 and iis 7. I am able to remove all bindings with Remove-ItemProperty, and when i use Set-ItemProperty it removes all binding and just adds the new. I would be great if i could just rename or just remove a single binding without effecting the others. A sample would be gre...

How does PUTTY/PLINK determine a command has returned?

Is it newline? prompt? What exactly? Trying to run powershell over plink and the command executes but plink doesn't recognise its finished and the session hangs. Most curiously though, the command executes successfully when sent through the shell (via Putty). However, when sent via plink, the same command hangs... Any ideas? ...

Batch/Powershell to determine network ID

i need to write a script that allows a network drive to be attached on a certain network ...

Is there a PowerShell code formatter / pretty printer?

I'm looking for a source code beautifyer for PowerShell programs. Ideally, it would be CLI based, but any solution is acceptable. I would like to avoid configuring a generic pretty printer tool; I'd like a solution that works for PowerShell out of the box. Is there such a thing? ...

$MyInvocation.MyCommand.Path is $null in PowerGUI script editor

When trying to debug my powershell script in the powerGUI script editor (2.0.0.1082) the $MyInvocation.MyCommand.Path is $null. It works when running the script via powershell. Running it in Powershell_ise.exe (on one of our servers) also works fine. Have anyone else had the same problem or know what's wrong? Here's my powershell ve...

Powershell in CruiseControl.net to backup existing folder before deploying new version of the code

Hi I would like to zip a bunch of files (.exe and .dll) before I overwrite them with the new build. Is there a simple way to zip files without using some sort of dll? Just creating a folder with the build number / date time stamp will also work great. How do I pass parameters from the cruise control build process into my Powershell...

What to monitor on SQL Server

Hi all I have been asked to monitor SQL Server (2005 & 2008) and am wondering what are good metrics to look at? I can access WMI counters but am slightly lost as to how much depth is going to be useful. Currently I have on my list: user connections logins per second latch waits per second total latch wait time dead locks per second ...