powershell

Creating a PowerShell job that executes a SQL command every 5 minutes

I'm creating a PowerShell script that I'm going to execute using Start-Job. The job should continuously run in the background until I tell it to stop using Stop-Job. It should execute a SQL command on a timer with a specified duration and output the results to the jobs pipeline so I can retrieve them using Receive-Job. Right now the j...

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run commands remotely. So PS remoting is working between the local server and remote server. But when I try to access a share on a 3rd server (di...

Framework for GUI on top of Powershell?

The Powershell team preaches using GUI on top of Powershell (GUI actions translated into Powershell commands in the background and executed). I love the idea, specially b/c it would make my GUI scriptable (it's nice to click click and then see a trace window with all the cmdlets that I've executed). My question is, do they provide some...

Powershell: using redirection within the script produces a unicode output. How to emit single-byte ASCII text?

I am using Sandcastle Helpfile Builder to produce a helpfile (.chm). The project is a .shfbproj file, which is XML format, works with msbuild. I want to automatically update the Footer text that appears in the generated .chm file. I use this snippet: $newFooter = "<FooterText>MyProduct v1.2.3.4</FooterText>"; get-content -Encodin...

Powershell scripts to backup SQL, SVN

I'm trying to use PowerShell to create some backups, and then to copy these to a web folder (or, in other words, upload them to a WebDAV share). At first I thought I'd do the WebDAV stuff from within PowerShell, but it seems this still requires a fair amount of "manual labour", ie: constructing HTTP requests. I then settled for creating...

Help on getting started with powershell - rewrite an old BAT + SQL file

Hi, I'm totally new to powershell, and I need some help to get started. What I need is to write a small script that backup a SQL database, but every time with different name (to keep the last 4-5 versions only). Right now I have a BAT, which just launch osql with a sql script, as below REM BAT file starts here "C:\Program Files\Microsof...

Constructing Active Directory entry using PowerShell works in IIS 6 but not IIS 7

The following line of PowerShell works with IIS 6 installed: $service = New-Object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC") However, with IIS 7 it throws the following error unless the IIS 6 Management Compatibility role service is installed: out-lineoutput : Exception retrieving member "ClassId2e4f51ef21dd47e...

Powershell - Get Variable from C# Cmdlet

I'm writing a C# Cmdlet that needs to get the value of a global script variable. How do I do it? I noticed that the Runspace has SessionStateProxy.GetVariable method. Can I access the runspace from a C# Cmdlet? Thanks! ...

Building a Snap-in Targeting PowerShell V1 and PowerShell V2

How do I build a PowerShell snap-in to target PowerShell v1 and v2? Given that v1 and v2 can't be installed side by side how can I reference the appropriate assemblies and build against them? Inevitably as things go on I'm going to be adding new PowerShell v2 specific features. So I'm going to need separate product builds targeting diff...

How to clear $Error in PowerShell?

Is there a way to clear the $Error variable that tracks errors in a PowerShell session? If so, how do you do it? I have tried: $error.clear In the PowerShell ISE on Windows 7 and the $Error array is still populated. ...

PowerShell scripts on GitHub

PowerShell $OutputEncoding defaults to ASCII. PowerShell by default represents strings in Unicode. When I create scripts using the ISE, they are created in Unicode. The following command sends text to a file in Unicode: echo Testing > test.txt When I push these files to GitHub, the code view chokes on them because they aren't UTF-8...

Powershell XPath selection

Hi, I'm looking to use powershell to alter xml. I haven't been able with powershell to copy xml using the xpath. I can load the xml with the following: $doc = new-object "System.Xml.XmlDocument" $doc.Load("XmlPath.xml") but after I get the XML I can't figure out how to list the xml with an xpath and create another xml file with wha...

Parse xml in powershell

Hi, I have the following xml: <?xml version="1.0" encoding="UTF-8"?> <sections> <section name="Options"> <item key="HLVersionControlWebServiceURL" value="http://www.personec.no/webservices/HLVersionControl/HLVersionControl.asmx" /> <item key="AltinnWebServiceURL" value="https://www.altinn.no/webservices/DataExchange.asmx" /> ...

how to open the last opened files in ISE at the starting

hi all, i wanted to automatically open the last opend files in ISE with posh script, so i tryed to save filepaths of these files like the following. $action = { $psISE.CurrentPowerShellTab.Files | select -ExpandProperty FullPath | ? { Test-Path $_ } | Set-Content -Encoding String -Path$PSHOME\psISElastOpenedFiles.txt Set-Content -Enco...

visual studio 2010 system.management.automation assembly reference

Been having trouble with visual studio 2010 I've added reference to the project and double checked the when i type using System.Management.Automation; at the top of the file. when I go to compile / build it .. it says that the infamous are you missing an assembly reference. any ideas? ...

PowerShell Copy-Item what I'm doing wrong?

Given the dir structure: x\Code x\Script\Backup.ps1 Backup.ps1 contains: $BackupDirectoy = "..\Backup" $CodeDirectory = "..\Code" function BackupCurrentVersion() { New-Item $BackupDirectoy -type directory -force Copy-Item $CodeDirectory -destination $BackupDirectory -recurse } BackupCurrentVersion I'm doing somet...

Start-job vs. Invoke-command -asjob

I'm trying to do basic background jobs in PowerShell 2.0, and I'm seeing different things with start-job and invoke-command -asjob. If I do this: start-job -scriptblock {get-process} I get a job object, but the child job (which is created automatically by start-job) always has a JobStateInfo of "NotStarted". this, however, works as e...

Start a program in active user session with PowerShell remoting

Is it possible to detect that a specific user has an open session on a computer and to open a process in that session so that the application can be interacted with by the user using PowerShell remoting? How would I go around detecting which users have sessions open on the machine and what their state is (active, idle, disconnected, etc...

Check if file exist and run a batch file in powershell?

This may be simple question, but I am new to Powershell and could not find a way to do it. Basically, I have to run a bat process if a specified file does not exist. The file name is in a patten like "mmddyyy.dat" in a folder, where mmddyyyy is today's month, day(0 prefix if < 10) and year. Pusdo codes would be something like this: $Fi...

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks ...