Skip to content

Commit d72b5f7

Browse files
see changelog for v0.4.0
1 parent b906e47 commit d72b5f7

12 files changed

+2037
-33
lines changed

PSRemoteOperations.psd1

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#
22
# Module manifest for module 'PSRemoteOperations'
33
#
4-
# Generated by: Jeff Hicks
5-
#
6-
# Generated on: 9/17/2018
7-
#
84

95
@{
106

117
# Script module or binary module file associated with this manifest.
128
RootModule = 'PSRemoteOperations.psm1'
139

1410
# Version number of this module.
15-
ModuleVersion = '0.3.6'
11+
ModuleVersion = '0.4.0'
1612

1713
# Supported PSEditions
1814
CompatiblePSEditions = @("Desktop")
@@ -24,7 +20,7 @@ GUID = '62bc09fe-38bf-426d-aa3c-e6c2cf6bb528'
2420
Author = 'Jeff Hicks'
2521

2622
# Company or vendor of this module
27-
CompanyName = 'JDH IT Solutions, Inc.'
23+
CompanyName = 'JDH Information Technology Solutions, Inc.'
2824

2925
# Copyright statement for this module
3026
Copyright = '(c) 2018 Jeff Hicks. All rights reserved.'
@@ -51,7 +47,7 @@ PowerShellVersion = '5.1'
5147
# ProcessorArchitecture = ''
5248

5349
# Modules that must be imported into the global environment prior to importing this module
54-
# RequiredModules = @()
50+
RequiredModules = @('PSScheduledJob')
5551

5652
# Assemblies that must be loaded prior to importing this module
5753
# RequiredAssemblies = @()
@@ -73,7 +69,7 @@ FunctionsToExport = 'New-PSRemoteOperation','Invoke-PSRemoteOperation','Get-PSRe
7369
'Register-PSRemoteOperationWatcher'
7470

7571
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
76-
CmdletsToExport = '*'
72+
CmdletsToExport = ''
7773

7874
# Variables to export from this module
7975
VariablesToExport = ''
@@ -96,13 +92,13 @@ PrivateData = @{
9692
PSData = @{
9793

9894
# Tags applied to this module. These help with module discovery in online galleries.
99-
# Tags = @()
95+
Tags = @("Remoting","ScheduledJob")
10096

10197
# A URL to the license for this module.
102-
# LicenseUri = ''
98+
LicenseUri = 'https://github.com/jdhitsolutions/PSRemoteOperations/blob/master/license.txt'
10399

104100
# A URL to the main website for this project.
105-
# ProjectUri = ''
101+
ProjectUri = 'https://github.com/jdhitsolutions/PSRemoteOperations'
106102

107103
# A URL to an icon representing this module.
108104
# IconUri = ''

PSRemoteOperations.psm1

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#requires -version 5.1
22

3-
#TODO : About topic help
4-
#TODO : add help
53
#TODO : Update Readme
6-
#TODO : what options are there for initialization section or including profiles?
74

85
#region main code
96

@@ -35,8 +32,12 @@ Function New-PSRemoteOperation {
3532
[ValidateNotNullorEmpty()]
3633
[string]$ScriptPath,
3734

35+
[Parameter(HelpMessage = "An array of objects to pass as arguments. Values are positional to your script or scriptblock.")]
3836
[Object[]]$ArgumentList,
3937

38+
[Parameter(HelpMessage = "A script block of commands to run prior to executing your script or scriptblock.")]
39+
[scriptblock]$Initialization,
40+
4041
[ValidateScript( {Test-Path -Path $_})]
4142
[Parameter(HelpMessage = "The folder where the remote operation file will be created.")]
4243
[string]$Path = $global:PSRemoteOpPath,
@@ -71,6 +72,10 @@ Computername = '$Computername'
7172
$out += "`n"
7273
}
7374

75+
if ($Initialization) {
76+
$out += "Initialization = '$Initialization'"
77+
$out += "`n"
78+
}
7479
$out += "}"
7580

7681
$out | Write-Verbose
@@ -100,12 +105,12 @@ Function Invoke-PSRemoteOperation {
100105
ValueFromPipelineByPropertyName
101106
)]
102107
[ValidatePattern("\.psd1$")]
103-
[ValidateScript( {Test-Path -Path $_})]
108+
[ValidateScript({Test-Path -Path $_})]
104109
[Alias("pspath")]
105110
[string]$Path,
106111

107112
[Parameter(HelpMessage = "Enter the path for the archived .psd1 file")]
108-
[ValidateScript( {Test-Path -Path $_})]
113+
[ValidateScript({Test-Path -Path $_})]
109114
[string]$ArchivePath = $global:PSRemoteOpArchive
110115
)
111116

@@ -123,7 +128,7 @@ Function Invoke-PSRemoteOperation {
123128
Write-Verbose "Comparing $parent to $ArchivePath"
124129
#The archive path and path for data file must be different
125130
if ($parent -eq $ArchivePath) {
126-
Write-warning "The archive path must be different from the path to the psd1 file."
131+
Write-Warning "The archive path must be different from the path to the psd1 file."
127132
#bail out
128133
Return
129134
}
@@ -147,12 +152,32 @@ Function Invoke-PSRemoteOperation {
147152
#run the command
148153
Try {
149154
if ($PSCmdlet.ShouldProcess($cPath)) {
150-
Invoke-Command @in -ConnectionUri http://localhost:5985/WSman -ErrorAction stop
155+
#create a session
156+
Write-Verbose "Creating a temporary local session"
157+
$tmpSession = New-PSSession -ConnectionUri http://localhost:5985/WSman -ErrorAction stop
158+
159+
if ($in.Initialization) {
160+
Write-Verbose "Initializing"
161+
$init = [scriptblock]::Create($in.Initialization)
162+
Invoke-Command -ScriptBlock $init -session $tmpSession
163+
$in.Remove("Initialization")
164+
}
165+
166+
$in.Add("ErrorAction","Stop")
167+
$in.Add("Session",$tmpSession)
168+
#invoke the command
169+
Write-Verbose "Invoking Command"
170+
Write-Verbose ($in | Out-String)
171+
Invoke-Command @in
172+
173+
Write-Verbose "Removing temporary session"
174+
$tmpSession | Remove-PSSession
151175
}
176+
$errormsg = "''"
152177
$Completed = $True
153178
}
154179
Catch {
155-
$errormsg = $_.exception.message
180+
$errormsg = """$($_.exception.message)"""
156181
$Completed = $False
157182
}
158183
Finally {
@@ -163,10 +188,10 @@ Function Invoke-PSRemoteOperation {
163188
164189
"@
165190
#append the result data to the data file.
166-
(Get-content -Path $cPath | Select-object -skip 1 | Select-Object -SkipLast 1 ).Foreach( {$resultData += "$_`n"})
191+
(Get-Content -Path $cPath | Select-Object -skip 1 | Select-Object -SkipLast 1 ).Foreach( {$resultData += "$_`n"})
167192

168193
$resultData += "Completed = '$completed'`n"
169-
$resultData += "Error = '$errormsg'`n"
194+
$resultData += "Error = $errormsg`n"
170195
$resultdata += "Date = '$((Get-Date).toUniversalTime()) UTC'`n"
171196

172197
$resultData += "}"
@@ -258,9 +283,9 @@ Function Register-PSRemoteOperationWatcher {
258283
[ValidateScript({Test-Path $_})]
259284
[string]$ArchivePath = $global:PSRemoteOpArchive,
260285

261-
[Parameter(Mandatory, HelpMessage = "Enter your username and credentials")]
286+
[Parameter(HelpMessage = "Enter your username and credentials")]
262287
[ValidateNotNullOrEmpty()]
263-
[PSCredential]$Credential,
288+
[PSCredential]$Credential = "$env:USERDOMAIN\$env:USERNAME",
264289

265290
[Alias("Option")]
266291
[Microsoft.PowerShell.ScheduledJob.ScheduledJobOptions]$ScheduledJobOption
@@ -277,8 +302,8 @@ Function Register-PSRemoteOperationWatcher {
277302
#guid regex
278303
$guidrx = "[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}"
279304

280-
get-childitem $in\*.psd1 |
281-
where-object {$_.name -match "^$($env:computername)_$guidrx" } |
305+
Get-Childitem $in\*.psd1 |
306+
Where-Object {$_.name -match "^$($env:computername)_$guidrx" } |
282307
Invoke-PSRemoteOperation -ArchivePath $out
283308
}
284309
Write-Verbose "Using data path: $path"
@@ -291,12 +316,13 @@ Function Register-PSRemoteOperationWatcher {
291316
MaxResultCount = 1
292317
ArgumentList = @($Path,$ArchivePath)
293318
Credential = $Credential
294-
InitializationScript = { Import-Module c:\scripts\PSRemoteOperations }
319+
InitializationScript = { Import-Module PSRemoteOperations }
295320
}
296321

297322
if ($ScheduledJobOption) {
298323
$jobParams.add("ScheduledJobOption",$ScheduledJobOption)
299324
}
325+
300326
Register-ScheduledJob @jobParams
301327

302328
Write-Verbose "Ending $($myinvocation.MyCommand)"

changelog.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog for PSRemoteOperations
22

3+
## v0.4.0
4+
5+
+ Added initialization script option to RemoteJob definition and execution
6+
+ Added `New-PSRemoteProfileScript` function
7+
+ Added `PSScheduledJob` as a required module
8+
+ Set default name for `-Credential` parameter of `Register-PSRemoteOperationWatcher` to `$env:username`
9+
+ Added help documentation
10+
+ Created Pester tests for the module
11+
312
## v0.3.0
413

514
+ Renamed function nouns to include a PSPrefix
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
external help file: PSRemoteOperations-help.xml
3+
Module Name: PSRemoteOperations
4+
online version:
5+
schema: 2.0.0
6+
---
7+
8+
# Get-PSRemoteOperationResult
9+
10+
## SYNOPSIS
11+
12+
Parse the contents of a PSRemoteOperation archive file.
13+
14+
## SYNTAX
15+
16+
```yaml
17+
Get-PSRemoteOperationResult [[-ArchivePath] <String>] [-Computername <String>] [-Newest <Int32>]
18+
[<CommonParameters>]
19+
```
20+
21+
## DESCRIPTION
22+
23+
This command will parse the archived PSRemoteOperation file. It will default to the archive path specified by $PSRemoteOpArchive if it has been defined. The default behavior is to process all files but you can limit the search by computername.
24+
25+
## EXAMPLES
26+
27+
### Example 1
28+
29+
```powershell
30+
PS C:\> Get-PSOperationResult -computername Think51
31+
32+
Computername : think51
33+
Date : 09/18/2018 17:19:35 UTC
34+
Scriptblock :
35+
Filepath : C:\scripts\SystemReport.ps1
36+
ArgumentsList :
37+
Completed : True
38+
Error :
39+
```
40+
41+
Get the result for computer THINK51 using the user-defined $PSRemoteOpArchive variable as the path.
42+
43+
## PARAMETERS
44+
45+
### -ArchivePath
46+
47+
Enter the path to the archive folder. This will default to the global variable PSRemoteOPArchive if it has been defined.
48+
49+
```yaml
50+
Type: String
51+
Parameter Sets: (All)
52+
Aliases: path
53+
54+
Required: False
55+
Position: 0
56+
Default value: $PSRemoteOpArchive
57+
Accept pipeline input: False
58+
Accept wildcard characters: False
59+
```
60+
61+
### -Computername
62+
63+
Enter a computername to filter on.
64+
65+
```yaml
66+
Type: String
67+
Parameter Sets: (All)
68+
Aliases:
69+
70+
Required: False
71+
Position: Named
72+
Default value: None
73+
Accept pipeline input: False
74+
Accept wildcard characters: False
75+
```
76+
77+
### -Newest
78+
79+
Select the newest X number of results.
80+
81+
```yaml
82+
Type: Int32
83+
Parameter Sets: (All)
84+
Aliases:
85+
86+
Required: False
87+
Position: Named
88+
Default value: None
89+
Accept pipeline input: False
90+
Accept wildcard characters: False
91+
```
92+
93+
### CommonParameters
94+
95+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
96+
For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
97+
98+
## INPUTS
99+
100+
### None
101+
102+
## OUTPUTS
103+
104+
### RemoteOpResult
105+
106+
## NOTES
107+
108+
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
109+
110+
## RELATED LINKS
111+
112+
[about_PSRemoteOperations]()
113+
114+
[Invoke-PSRemoteOperation]()
115+
116+
[New-PSRemoteOperation]()

0 commit comments

Comments
 (0)