powershell-v2.0

Error trying to loop through $args in script file using range notation for loop index

I can't figure out why the following code fails: # test.ps1 "`$args: ($args)" "`$args count: $($args.length)" # this fails 0..$($args.length - 1) | %{ $args[$_] = ($args[$_] -replace '`n',"`n") } # this works $i = 0 foreach ( $el in $args ) { $args[$i] = $args[$i] -replace '`n',"`n"; $i++ } "$args" I'm calling it like so: rem from ...

How to get PowerShell to display registry Data values

Take the Winlogon registry section, I would like PowerShell to display the Data value for DefaultUserName. This is as far as I have got: Stage 1 get-itemproperty -path "hklm:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\" Stage 2 I can append: -Name DefaultUserName But this won't return a value. Also other Names, despite...

Sql Command (Exception calling "ExecuteScalar" with "0" argument)

When I run the below code on first attempt I get an unexplained error, but running the script again on a second attempt works fine...what would be wrong in my code? By the way I am creating the database before this step... $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server=$dBS...

check RAM,page file, /PAE, /3GB, SQL server memory using powershell

I am a powershell novice. After days of searching.... I have put together a small powershell script (as below) to check page file, /PAE switch, /3GB switch, SQL server max RAM, min RAM. I am running this on 1 server. If I want to run it on many servers (from a .txt) file, How can I change it ? How can I change it to search boot.ini fi...

64bit equivalent class for a wmi class "win32reg_addremoveprograms"?

My code below works perfectly on a 32-bit Windows machine, however it refuses to work on a 64-bit machine due to the 32-bit WMI class win32reg_addremoveprograms used in the code. Is there a 64-bit equivalent of this class? $ServerFile = "D:\SharePoint\Powershell\AddRemovePrograms\Machines.txt" $ServerList = Get-Content $ServerFile $Exc...

How to read the xml file and write its content into a plain text file?

How to read the below xml file and write its content into a plain text file? <!-- < config > < compare > < source >d:\demo\< /source > < destination >e:\demo\< / destination > < parameters > < parameter >FileName< /parameter> < parameter >Size< /parameter> < parameter>Date< /parameter> < /parameters > < /compare > < ...

pass username and password to get-credential or run sql query without using invoke-sqlcmd in Powershell

I am trying to connect to a remote sql database and simply run the "select @@servername" query in Powershell. I'm trying to do this without using integrated security. I've been struggling with "get-credential" and "invoke-sqlcmd", only to find (I think), that you can't pass the password from "get-credential" to another Powershell cmdle...

Cannot Generate ParameterSetMetadata While Programmatically Creating A Parameter Block

I'm trying to programmatically create a parameter block for a function ( along the lines of this blog post ). I'm starting with a CommandMetadata object (from an existing function). I can create the ParameterMetadata object and set things like the ParameterType, the name, as well as some attributes. The problem I'm running into is t...

How to implement a password change check in Powershell?

I've created a set of virtual machines (Windows Server) with a specific admin password; these VMs have been assigned to users, and may be in use. I want to know if the user changed the admin password, and do the check so the user doesn't notice. What are good solutions in powershell? ...

tabexpansion doesn't fall through if overridden

The tabexpansion function only works partially when I override it like so: function tabexpansion { param($line, $lastWord) if ($line -eq "hey ") { "you", "Joe" } } The custom completions work as expected, but now I only get the default autocomplete behavior for cmdlet names, not parameters. So New-TAB works fine, ...

Powershell: How do I pass a log4net object to a background job?

I may be trying an invalid approach, so I'm open to any suggestions. I'm running a series of 3 scripts that each do an analysis of websites on an IIS server, and I'm running them against a couple hundred servers. I'm proof-of-concepting doing this as a Start-Job process so I can run in parallel and finish a lot more quickly. These scri...

Switch -Regex in Powershell.

$source |% { switch -regex ($_){ '\<'+$primaryKey+'\>(.+)\</'+$primaryKey+'\>' { $primaryKeyValue = $matches[1]; continue; } } I want to use dynamic key value with switch-regex, is that possible? ...

XML Document Depth?

How to find the depth of the xml file using powershell/xpath? consider the below xml: <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title>Harry Potter</title> <price>25.99</price> </book> <book> <title>Learning XML</title> <price>49.95</price> </book> </bookstore> depth of the above xml document is 3 (bookst...

Get-QADComputer -LdapFilter & variables

Can I use a variable in and LdapFilter with Get-QADComputer? i.e.: $31DaysAgo = (Get-Date).AddDays(-31) $ft = $31DaysAgo.ToFileTime() $StComps = Get-QADComputer -SizeLimit 0 -IncludeAllProperties -SearchRoot ` 'DC=MY,DC=DOMAIN,DC=LOCAL' -LdapFilter '(&(objectcategory=computer) ` (pwdLastSet<=$ft)(|(operatingsystem=Windows 2000 Profess...

How can you create an instance of the System.Management.Automation.ProviderInfo

I'm starting to create a PowerShell Provider. To start off, I have a class inheriting from NavigationCmdletProvider. There's only one protected constructor and it requires an instance of ProviderInfo. Yes, that's ProviderInfo .ctor requiring instance of ProviderInfo. In creating my first test around this provider, I can't seem to find ...

Copy files where lastwritetime -ge 3/26/2010 9:00pm with Powershell

I need to copy files in one directory to another directory where the lastwritetime is greater than or equal to 3/26/2010 9:00pm. I'm using: Get-ChildItem C:\pstest\hlstore\folder1\data | where-object {$i.lastwritetime -ge “3/26/2010 9:00 PM”} | Copy-Item -destination c:\pstest\hlstore2\folder1\data But nothing happens... Any help wo...

How to run PowerShell scripts via automation without running into Host issues

I'm looking to run some powershell scripts via automation. Something like: IList errors; Collection<PSObject> res = null; using (RunspaceInvoke rsi = new RunspaceInvoke()) { try { res = rsi.Invoke(commandline, null, out errors); } catch (Exception ex) { LastErrorMessage = ex.ToString(); Debug....

How can I get PowerShell Added-Types to use Added Types

I'm working on a PoSh project that generates CSharp code, and then Add-Types it into memory. The new types use existing types in an on disk DLL, which is loaded via Add-Type. All is well and good untill I actualy try to invoke methods on the new types. Here's an example of what I'm doing: $PWD = "." rm -Force $PWD\TestClassOne* $code...

Best practices for writting PowerShell scripts for local and remote usage

What are some of the best practices for writting scripts that will execute in a remote context? For instance, I just discovered that built-in var $Profile doesn't exist during remote execution. ...

IIS 7.5 powershell module usage issues

Has anyone managed to use this module with success, i'm running 32bit Windows 7, where i have opened an administrator shell using run as administrator, i have imported the WebAdministration module and then attempted to use the commands with some issues, have provided two examples here Websites I created a web site with the following co...