Welcome to Powergui.org - an open source community for Windows Powershell

PowerGUI.org PowerGUI.org and blogs

Forums » PowerGUI User Discussions

Thread: Cmdlets in scripts?

This question is answered. Helpful answers available: 2. Answered answers available: 1.


Permlink Replies: 8 - Pages: 1 - Last Post: Aug 19, 2009 6:05 PM by: Jspooner
Jspooner

Posts: 4
Registered: 8/13/09
Cmdlets in scripts?
Posted: Aug 13, 2009 10:33 PM
 
  Click to reply to this thread Reply

I am very new to powershell and i like some of the powerpacks in the powergui. My question is how do i use these cmdlets in a powershell script. I seem to need a snapin it seems but i can not find many snapins on the internet. I have found some instructions how to install a snapin but i am truely confused. I want to extend powershells cmdlet library to include some of these powergui cmdlets so that i can do more at the script level. The cmdlet i would like to use in a script most is the get-oldfiles in the file system Management powerpack. Some one please set me straight.
Message was edited by: Jspooner


hugopeeters


Posts: 22
Registered: 5/27/09
Re: Cmdlets in scripts?
Posted: Aug 14, 2009 1:07 AM   in response to: Jspooner
 
  Click to reply to this thread Reply

Get-OldFiles is not a cmdlet, it's a script. Therefor, it is not available in a snapin.


seaJhawk


Posts: 414
Registered: 12/15/08
Re: Cmdlets in scripts?
Posted: Aug 14, 2009 8:02 AM   in response to: hugopeeters
 
  Click to reply to this thread Reply

Get-OldFiles is not available in a snapin, but you could certainly lift it from the PowerPack. Just save the script below into a file called Get-OldFiles.ps1.

#####################################################################

trap

{

Write-Output $("TRAPPED: " + $_.Exception.GetType().FullName);

Write-Output $("TRAPPED: " + $_.Exception.Message);

continue;

}

Function Get-PromptParam($name, $mandatory, $defaultValue, $intg)

{

$param = ([System.Management.Automation.Host.FieldDescription][string]$name)

$param.IsMandatory = $mandatory

$param.DefaultValue = $defaultValue

if($intg) {$param.SetParameterType([Type]"System.Int32")}

$param

}# End Function Get-PromptParam

Function PromptForParams($Message){

$genericType = [Type]("System.Collections.ObjectModel.Collection" + '`' + "1")

$closedType = $genericType.MakeGenericType([Type]"System.Management.Automation.Host.FieldDescription")

$AskFieldCol = [Activator]::CreateInstance($closedType, $false)

$AskFieldCol.Add((Get-PromptParam "Directory" $true $($global:fdir) $false))

$AskFieldCol.Add((Get-PromptParam "Days" $true ($global:rdays) $true))

$host.ui.Prompt("",$Message,$AskFieldCol)

}

# End Function PromptForParams

$today = [DateTime]::Now

if (($global:fdir -eq "") -or ($global:fdir -eq $null) -or ($global:fdir -eq 0) -or ($global:rdays -eq $null))

{

$p = PromptForParams "Enter the [Directory/Share] you want to process and minimum [Age(in Days)] of files."

$global:fdir = $p["Directory"]

$global:rdays = $p["Days"]

}

$directory = $($global:fdir).Replace("'","")

# days old or older the file must be

$days = -$global:rdays

 

$oldfiles = get-ChildItem -recurse -Force -ErrorAction SilentlyContinue -Path $directory | ? { $_.GetType().Name -eq "FileInfo" } | ? { $today.AddDays($days) -gt $_.LastWriteTime } | Sort-Object -Property LastWriteTime

$oldfiles

#####################################################################




Jspooner

Posts: 4
Registered: 8/13/09
Re: Cmdlets in scripts?
Posted: Aug 14, 2009 9:54 AM   in response to: seaJhawk
 
  Click to reply to this thread Reply

Pulling the script was what i was thinking of too. I have tried doing that and saved it as c:\scripts\Get-Oldfiles.ps1. I then set up a new script and entered this

c:\Scripts\get-oldfiles.ps1 c:\test 40. The output was not to be expected. It seemed to be scanning the whole c: and it did not seem to use the parameters. So my question now would be how do i pass the parameters to the new Get-Oldfiles.ps1 script? I told you i was new didn't I. Thank you.

Message was edited by: Jspooner


seaJhawk


Posts: 414
Registered: 12/15/08
Re: Cmdlets in scripts?
Posted: Aug 14, 2009 10:12 AM   in response to: Jspooner
 
  Click to reply to this thread Reply

ok - I updated it a bit so you can pass the following parameters:

-minimumAge
-Directory
-Recurse

By default the script won't recurse (search subfolders). If you want it to recurse then pass the -Recurse switch parameter.


Jspooner

Posts: 4
Registered: 8/13/09
Re: Cmdlets in scripts?
Posted: Aug 14, 2009 2:11 PM   in response to: seaJhawk
 
  Click to reply to this thread Reply

Thank you for the edits you made. Unfortunately i have not yet been able to try it out. When i went to run it with the new parameters you set i recieved the following.

File C:\Scripts\Get-Oldfiles.ps1 cannot be loaded. The file C:\Scripts\Get-Oldfiles.ps1 is not digitally signed. The script will not execute on the system. Please see "get-help about_signing" for more details..

I am looking into the details now but if you had a quick solution i would appreciate it. I did set my assigned policy to remote.




iota

Posts: 2
Registered: 8/14/09
Re: Cmdlets in scripts?
Posted: Aug 15, 2009 12:30 AM   in response to: Jspooner
 
  Click to reply to this thread Reply

To know your execution policy use Get-ExecutionPolicy

To set no restriction use Set-ExecutionPolicy -executionPolicy Unrestricted

But you should have a look to this article about signing scripts and getting more secure environment :

http://technet.microsoft.com/en-us/magazine/2008.04.powershell.aspx?pr=blog

Cheers


seaJhawk


Posts: 414
Registered: 12/15/08
Re: Cmdlets in scripts?
Posted: Aug 15, 2009 3:38 AM   in response to: Jspooner
 
  Click to reply to this thread Reply

Rather than changing your execution policy which can open you up to problems, just create a new file called Get-OldFiles.ps1 and copy the text from the .ps1 I posted into this new file.

-Chris




Jspooner

Posts: 4
Registered: 8/13/09
Re: Cmdlets in scripts?
Posted: Aug 19, 2009 6:05 PM   in response to: seaJhawk
 
  Click to reply to this thread Reply

Just wanted to thank you all for your help. I finally got what i wanted and for being new at powershell this was an empowering experience. Both powershell and this forum rock. I like this Get-oldfiles program because it allowed me to maintain many clean up routines of backup directories. Thank you again.




Legend
MVP: 2501 + pts
Guru: 2001 - 2500 pts
Expert: 751 - 2000 pts
Enthusiast: 31 - 750 pts
Novice: 0 - 30 pts
Moderators
Helpful answer (5 pts)
Answered (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums