powershell

powershell check sharepoint URLs

I'm trying to write a script that iterates through a bunch of sharepoint URLs and verifies that they exist. From what I can find, it looks like this should work: $webclient = new-object System.Net.WebClient $webclient.Credentials = new-object System.Net.NetworkCredential ("username", "password", "domain") $webpage = $webclient.Downlo...

PowerShell cmdlet parameter validation

I'm writing a custom PowerShell cmdlet, and I would like to know which is the proper way to validate a parameter. I thought that this could be done either in the property set accessor or during Cmdlet execution: [Cmdlet(VerbsCommon.Add,"X")] public class AddX : Cmdlet { private string _name; [Parameter( Mandatory=false...

Format of File using Awk in PowerShell

All I did was ls to a file, then ran a simple awk print. I'm new to both PowerShell and Awk, but the output is obsviously not what's expected. Can anyone explain this? Does it have something to do with the format of the file? PS C:\Documents and Settings\lmoser\My Documents\Test> awk '{ print }' lsfiles.txt > awkedlsfile.txt PS C:\Do...

Should I implement the IPropertyCmdletProvider interface for a PowerShell Cmdlet?

I'm writing a NavigationCmdletProvider for PowerShell. Through the GetItem and GetChildItems overrides, there are various types of objects that are written to the pipeline. The docs for IPropertyCmdletProvider interface tell us the following: Developers should implement this interface under the following conditions: When...

Referencing external assemblies from PowerShell snap-in

I'm developing a custom PowerShell snap-in, which references another project in the solution. When I try to debug the snap-in (following [these instructions][1]), the assembly fails to load and my cmdlet fails with the message "Could not load file or Assembly..." How do you instruct PowerShell on how to locate assemblies, or how do yo...

How to build a WPF application with Add-Type C# code

hi, i tryed to build WPF application using Add-Type, but it failed. $a = @' using System.Windows; public class Program { [STAThread()] public static void Main(string[] args) { Application app = new Application(); Window1 w = new Window1(); app.Run(w); } ...

User powershell to get a list of all folders in /downloads/pivots

So I have a boat load of pivot tables that I upload everyday to folders like: Pivot0001 Pivot0002 Pivot0003 and so on. I also have user groups called Pivot0001 and so on with users that need to access that folder in it. What I now need to do is set the permissions on each folder (I have 400 or so of them). I know I need to do a loop ...

How to redefine prompt function during Powershell session

Is it possible to redefine the prompt function specified in the user's profile after the shell has started? ...

Writing data to cells in Excel 2007 / PowerShell

Hello, Why can I not write values to Excel using the Worksheet class, or Sheet interface? I would expect to be able to do something like this: [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel") $Excel = New-Object Microsoft.Office.Interop.Excel.ApplicationClass $Workbook = $Excel.Workbooks.Add() $Worksheet = $...

Continous Integration: PowerShell vs. CI Server (CC.NET or Hudson)

So, a friend and I have been discussing continuous integration and bat/powershell scripts versus CI servers like CruiseControl.Net or Hudson. The following powershell pseudo script works to update from SVN, build using msbuild, deploy/copy out, update a build/revision number in the app, and emails on failed builds. The next step would ...

C# - Checking For Device Problems and System Problems

In C#, How Could I go about checking for device and systems errors? Would it be simple to use PowerShell Scipts, or would that add to the complexity and difficulty? ...

Showing the UAC prompt in PowerShell if the action requires elevation

I have a simple PowerShell script to stop a process: $p = get-process $args if ( $p -ne $null ) { $p | stop-process $p | select ProcessName, ID, HasExited, CPU, Handles } else { "No such process" } If I try to stop a process not started by the current user; it works on Windows Server 2003. However, on Windows Server 2008 (and other Wi...

Convert Keith Hill's PowerShell Get-Clipboard and Set-Clipboard to a PSM1 script

I'd like to convert Keith Hill's C# implementation of Get-Clipboard and Set-Clipboard into pure PowerShell as a .PSM1 file. Is there a way to spin up an STA thread in PowerShell as he does in his Cmdlet when working with the clipboard? The Blog Post The Code ...

Powershell 1.0 Excel Automation - Problem with "Font.ColorIndex"

I’m trying to automate Excel in Powershell 1.0 and am having problems trying to apply a cells “Font.ColorIndex” property. The following Microsoft KB article details a BUG with Excel automation when the computer running the script has a locale setting other than “en-us” My example script below works perfectly when I manually change my l...

Passing directory contents as a single line to an executable in Powershell

I have a binary executable that takes a list of file paths as arguments, e.g., C:\Tool.exe C:\Files\File1.txt C:\Files\File2.txt I would like to call this tool from Powershell. The question is, how can I get the output of get-childitem all on one line? If I run: ls C:\Files\*.txt | select FullName I get one path per line. H...

Windows PowerShell authentication

I am running PowerShell 1.0 under Windows XP, attempting to connect to machines running XP, Vista, and Server 2003 with the following command: gwmi -cl Win32_OperatingSystem -co COMPUTER -n "root\CIMV2" -cr DOMAIN\ADMIN This returns an error. "Exception retrieving members: Access is denied.". gwmi -cl Win32_OperatingSystem -co COMPUT...

How to determine the binding order of network interfaces on Windows using .NET?

I'm trying to find the magical answer to the "what's the primary IP address" of a system. My research has determined that the answer to "best" means this: Look at [system.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() for interfaces with default gateways assigned. If you only find one, then you are done. If there's...

How do I accurately report errors when using PowerShell's Measure-Command

When I have an error inside a script block that I'm testing with Measure-Command, the error is show at the line of the invocation, and not at the location the error is thrown. Here's an example: function ThrowAnError { Write-Host "in ThrowAnError" throw "ThrowAnError" } Measure-Command { ThrowAnError } This reports At Scrip...

How to delete empty subfolders with PowerShell

I have a share that is a "junk drawer" for end-users. They are able to create folders and subfolders as they see fit. I need to implement a script to delete files created more than 31 days old. I have that started with Powershell. I need to follow up the file deletion script by deleting subfolders that are now empty. Because of the ...

Build process to publish with a Visual Studio 2008 application using PowerScript

I'm am trying to find out how to use PowerScript to publish my Visual Studio 2008 web application, any points would be most welcomed ...