powershell

Powershell "join"

Call me stupid, but I'm losing my hair with this one. I have two results from a Get-WmiObject: $cpu = Get-WmiObject -Class Win32_Processor $mb = Get-WmiObject -Class Win32_BaseBoard Now, I can filter and output a CSV file from each one: $cpu | Select-Object Name, Description | ConvertTo-Csv -NoTypeInformation and $mb | Select-Ob...

Use UFormat to get unix time

I can use following to append a date to a text: "Foo {0:G} Foo" -f (date) #returns "Foo 2009-12-07 15:34:16 Foo" But I want the time in Unix format. I can get it by date -UFormat %s, but can I use the same syntax? When I use -UFormat %s I get 1260199855,65625, how do I remove the decimal? ...

Powershell regex question involving balancing parenthesis

Hi, How can I convert a table column definition into an array of columns using regex's without taking formatting into account? I'm confused how to split on "," as they can also appear in the datatype definition part (between matching parenthesis). Sample input: CREATE table test ( DISTRICT VARCHAR(3) CHARACTER SET LATIN NOT CASESPECIF...

Powershell: get-ChildItem recurse as a parameter

I am relatively new to PowerShell, so I am hoping this is simple, and I just missed it. I am looking to create a function that could toggle the ability to recurse in get-childItem. As a very basic example: ... param ( [string] $sourceDirectory = ".", [string] $fileTypeFilter = "*.log", [boolean] $recurse = $true ) ge...

Powershell challenge

I've been challenged by a friend to do the following: "Find the quickest and easiest way of sorting a directory listing by the LAST character of the filenames." He's done it on Linux using the following: ls | rev | sort | rev I'd like to show him the powershell alternative, but I'm only just starting to learn powershell and I can't ...

Load variables from another powershell script

I have several scripts that could be reusing variables so I'd like to isolate variables in their own Variables.ps1 script, i.e. $var1 = "1" $var2 = "2" I'm trying to load these variables then print them out in the Main.ps1 script like this: .\Variables.ps1 $var1 $var2 This works if I first run .\Variables.ps1 but not if I just run ...

Windows Powershell SDK and System.Management.Automation.PSObject

I have a build error in a c sharp program that I am compiling in Visual Studio 2008 on a Windows Server (2008, I guess) SP 2 64-BIT OS. It says that 'System.Management.Automation.PSObject' is defined in an assembly that is not referenced. I did some searching in MSDN and I found that this seems to be part of the Windows Power Shell SDK. ...

Upload files with ftp using powershell

I want to use powershell to transfer files with FTP to an anonymous FTP server. I would not use any extra packages. How? There must be no risk that the script hangs or crashes. ...

I cannot debug in PowerGui

I have PS V2 and PowerGUI (1.9.6.1027). I set a debug point in my script in PowerGUI, but I cannot get debug workin when I start debug from tool bar or menu item. I got a dialog of "Continue with this operation?" as caption. There are 4 options "Yes", "Yes to All", "NO" and "No to All". Not sure if there is any thing or option I have to...

powershell v2 remote features?

Just listened to Hansellminutes podcast. He had a talk with two Microsoft PS developers. They mentioned PS V2 remoting features. I have some scripts based on PS v1. In terms of remoting commands or executions, I installed PS on local and a remote machines. Then I use PsExec.exe to push bat on remote to execute PS scripts. Now I am think...

PowerShell: how *exactly* does the RHS of the -f operator work?

Last time I got confused by the way PowerShell eagerly unrolls collections, Keith summarized its heuristic like so: Putting the results (an array) within a grouping expression (or subexpression e.g. $()) makes it eligible again for unrolling. I've taken that advice to heart, but still find myself unable to explain a few esoterica. ...

Insert Content into Text File in Powershell

I want to add content into the middle of a text file in Powershell. I'm searching for a specific pattern, then adding the content after it. Note this is in the middle of the file. What I have currently is: (Get-Content ( $fileName )) | Foreach-Object { if($_ -match "pattern") { #Add Line...

VARBINARY output in MS MGMT Studio different than Powershell/.NET

I'm in a bit of a situation with outputting data from a table where the only column I'm selecting is a VARBINARY(MAX) type. In management studio, when I execute the query, I get back what I expect in the format: 0x1FABCDEFG......etc Now, when the same query is executed in powershell, via a simple setup of a SqlCommand, SqlDataAdapter...

What is the best file parsing solution for converting files?

I am looking for the best solution for custom file parsing for our enterprise import routines. I want to basically change one file format into a standard file format and have one routine that imports that data into the database. I need to be able to create custom scripts for each client since its difficult to get the customer to comply w...

When should 'Unknown' be used as -Encoding parameter?

Hi all, I was thinking Unknown option is used for binary files concatenation. http://technet.microsoft.com/en-us/library/dd315299.aspx Unknown The encoding type is unknown or invalid. The data can be treated as binary. But {Get-Content binary.dat -Encoding Unknown} doesn't return byte array but string array. PS > $a = Get-Co...

PowerShell: read lines from text file, construct source and destination file names, then copy files

I'm a PowerShell novice, and I'd love to be able to script this. I have a text file where each line is part of a file name without the path or extension. I'd like a one-liner that loops through each line of the file (with a gc - Get-Content, right?), takes the content of the line, constructs the source path (the network drive and exten...

Copy-Item copies directory as well as contents to UNC path

Im trying to take the conents of a folder and copy it to a another unis powershell 1.0. Pretty simple stuff and all works fine using Copy-Item $from $to -recurse if I am copying from a local folder to a local folder, however if the $to variable is a UNC path it seems to copy the $from directory, not just its contents eg $from = "c:\temp...

How to check FtpWebRequest for errors

If I use System.Net.FtpWebRequest to upload a file to a vsftpd server, do I need to use GetResponse to check if the file was uploaded correctly? Or do I get an exception for every error? What in System.Net.FtpWebResponse should I check on? ...

How do I use a variable in a UNC path in powershell?

Hi All, and thanks in advacne for your help large or small. A bit of a newbie to the powershell world and I'm trying to count the contents of a folder on a remote server. I know that: Get-ChildItem \\ServerName\c$\foldername -recurse | Measure-Object -property length -sum works a treat. However I'm trying to make the server name a...

Export-Mailbox cmdlet not deleting emails

Hi, I've got an issue with using Export-Mailbox cmdlet in Powershell to delete emails between two dates from a mailbox. The current code that I am running is: Export-Mailbox -Identity mailboxname -StartDate "01/11/2009 00:00:01" -EndDate "14/11/2009 00:00:01" -DeleteContent I know there are emails that fall between these two dates, ...