powershell

How can I check if a powershell script is signed from the managed API?

I want to execute a powershell using the .net Powershell SDK. I have this working fine. Before I execute it I want to check that the script has been signed by my code signing certificate - this is easy enough to do from within powershell itself using Get-AuthenticodeSignature but I would like to do this in code before choosing to execu...

Powershell: Select-Object with additional calculated difference value

Let me explain my question. I have a daily report about one PC's free space as in text file (sp.txt) like: DateTime FreeSpace(MB) ----- ------------- 03/01/2010 100.43 DateTime FreeSpace(MB) ----- ------------- 03/02/2010 98.31 .... Then I need to use this file as input to generate a report wi...

Sql Server SMO connection timeout not working

I have the following PowerShell code: function Get-SmoConnection { param ([string] $serverName = "", [int] $connectionTimeout = 0) if($serverName.Length -eq 0) { $serverConnection = New-Object ` Microsoft.SqlServer.Management.Common.ServerConnection } else { $serverConnection...

How to stop PowerShell from unpacking an Enumerable object?

Working on a simple helper function in PowerShell that takes a couple of parameters and creates a custom Enumerable object and outputs that object to the pipeline. The problem I am having is that PowerShell is always outputting a System.Array that contains the objects that are enumerated by my custom Enumerable object. How can I keep P...

Toggle Local Area Connection Property (check box) using Power Shell

I have the Check Point VPN client installed and frequently must toggle the local area connection property (check box) on and off. I have looked at doing this with Power Shell and have found the following. $colItems = get-wmiobject -query "Select * From Win32_NetworkAdapterConfiguration" | Where-Object {$_.Description -like "Secu"} fore...

Calculate directed broadcast address given IP address and subnet in PowerShell

My goal is to calculate the directed broadcast address when given the IP and subnet mask of a host node. I know, sounds like homework. Once I reasoned through my task and boiled it down to this, I was amused with myself. Anyway, the solution will look something like the one in this question I suppose, but I'm not a math major and my C su...

PowerShell: How to find and uninstall a MS Office Update

I've been hunting for a clean way to uninstall an MSOffice security update on a large number of workstations. I've found some awkward solutions, but nothing as clean or general like using PowerShell and get-wmiobject with Win32_QuickFixEngineering and the .Uninstall method on the resulting object. [Apparently, Win32_QuickFixEngineeri...

How to test for existence of a script-scoped variable in PowerShell?

Is it possible to test for the existence of a script-scoped variable in PowerShell? I've been using the PowerShell Community Extensions (PSCX) but I've noticed that if you import the module while Set-PSDebug -Strict is set, an error is produced: The variable '$SCRIPT:helpCache' cannot be retrieved because it has not been set. At C:\Use...

Stdin to powershell script

I have a service running that can invoke an external process to modify a text stream before it is returned to the service. The text stream is handed from the service to the external process on stdout and the modified result is read from the service on stdin. The external process (command) can in other words be used as a text "filter". I ...

Executing Powershell from Perl

Hi, I have a bunch of Powershell scripts which I need to run from Perl. I have the following code but for some reason the Powershell scripts dont get invoked. I have tried both the backtick and the system command $path = "C:/Users/PSScript.ps1"; $pwspath = "c:/windows/system32/windowspowershell/v1.0/powershell.exe"; $output = `$pwspat...

PowerShell FileInfo outputs file in child directory

Why is the output in this case not c:\source\temp\test.txt? PS C:\source\temp> (New-Object IO.FileInfo .\test.txt).FullName c:\source\test.txt ...

Where are the default aliases defined in powershell?

This may be a stupid question, but are the default aliases (e.g. cd) hardcoded in powershell or defined in a hidden "profile" script somewhere? I don't have any profiles set (per-user or system-wide) so I'm just wondering where the default ones come from. ...

How does a cmdlet know when it really should call WriteVerbose()?

How does a cmdlet know when it really should call WriteVerbose(), WriteDebug() and etc.? Perhaps I miss something simple but I cannot find the answer. All cmdlet implementations I have seen so far just call WriteVerbose() without any hesitation. I know that it is correct to do so, but it is not effective. Performance suffers when verbo...

XmlSerializer's functionality in PowerShell?

Is there a way to leverage the functionality of .NET's XmlSerializer class in PowerShell? More specifically, the ability to easily de/serialize .NET types into Xml e.g. XmlSerializer s = new XmlSerializer(typeof(MyType)); TextReader r = new StreamReader("sample.xml"); MyType myType = (MyType)s.Deserialize(r); r.Close(); I know I can ...

PS: Filter selected rows with only max values as output?

I have a variable results ($result) of several rows of data or object like this: PS> $result | ft -auto; name value ---- ----- a 1 a 2 b 30 b 20 .... what I need to get all the rows of name and max(value) like this filtered output: PS> $result | ? |ft -auto name value ---- ----- a ...

Output from external exe and my custom objects in powershell

(Sorry for strange title, haven't come up with anything better..) Background I use nunit-console to test my assemblies. It is called like this (simplified): function Test-ByNunit { param($assembly, $tempFile = 'c:\temp\nunit.xml') & <path-to-nunit-console> $assembly /nologo /xml:$tempFile @othparam } Test-ByNunit c:\temp\myAs...

Output redirection still with colors in PowerShell

Suppose I run msbuild like this: function Clean-Sln { param($sln) MSBuild.exe $sln /target:Clean } Clean-Sln c:\temp\SO.sln In Posh console the output is in colors. That's pretty handy - you spot colors just by watching the output. And e.g. not important messages are grey. Question I'd like to add ability to redirect it some...

Should a data warehouse developer know Powershell scripting?

I am a SQL Server (2005 & 2008) data warehouse developer (SSIS, SSAS, SSRS, SQL) and I am wondering if it would be worth the effort and time to learn Powershell scripting. Are there applicable uses from a development perspective? I understand that from a DBA view there is considerable power in Powershell for administration - does any o...

Find and Replace in a Large File

I want to find a piece of text in a large xml file and want to replace with some other text. The size of the file is around ( 50GB). I want to do this in command line. I am looking at Powershell and want to know if it can handle the large size. Also I would like to the know the syntax for escaping the key operators in powershell. I am a ...

Checking only "Automatic" services with powershell.

I've seen lots of scripts out there for manually stopping/starting services in a list, but how can I generate that list programatically of -just- the automatic services. I want to script some reboots, and am looking for a way to verify that everything did in fact start up correctly for any services that were supposed to. ...