|
Replies:
8
-
Pages:
1
-
Last Post:
Aug 19, 2009 6:05 PM
by: Jspooner
|
|
|
Posts:
4
Registered:
8/13/09
|
|
|
|
Cmdlets in scripts?
Posted:
Aug 13, 2009 10:33 PM
|
|
|
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
|
|
|
Posts:
22
Registered:
5/27/09
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 14, 2009 1:07 AM
in response to: Jspooner
|
|
|
Get-OldFiles is not a cmdlet, it's a script. Therefor, it is not available in a snapin.
|
|
|
Posts:
414
Registered:
12/15/08
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 14, 2009 8:02 AM
in response to: hugopeeters
|
|
|
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 #####################################################################
|
|
|
Posts:
4
Registered:
8/13/09
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 14, 2009 9:54 AM
in response to: seaJhawk
|
|
|
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
|
|
|
Posts:
414
Registered:
12/15/08
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 14, 2009 10:12 AM
in response to: Jspooner
|
|
|
|
|
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.
|
|
|
Posts:
4
Registered:
8/13/09
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 14, 2009 2:11 PM
in response to: seaJhawk
|
|
|
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.
|
|
|
Posts:
2
Registered:
8/14/09
|
|
|
Posts:
414
Registered:
12/15/08
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 15, 2009 3:38 AM
in response to: Jspooner
|
|
|
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
|
|
|
Posts:
4
Registered:
8/13/09
|
|
|
|
Re: Cmdlets in scripts?
Posted:
Aug 19, 2009 6:05 PM
in response to: seaJhawk
|
|
|
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)
|
|