functions/private/Invoke-Operation.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function Invoke-Operation {
    [cmdletbinding()]
    param(
        [switch] $Wait,
        [scriptblock] $Code ={},
        [hashtable] $Parameters,
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [hashtable] $SyncHash,
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $Root,
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $Log
    )
    try {
        if(!$Root){
            if($SyncHash.Root){
                $Root = $SyncHash.Root
            } else {
                throw "Invalid Root"
            }
        }
        if(!$Log){
            if($SyncHash.Log){
                $Log = $SyncHash.Log
            } else {
                throw "Invalid Log"
            }
        }
        $Runspace = [runspacefactory]::CreateRunspace()
        $Runspace.ApartmentState = "STA"
        $Runspace.ThreadOptions = "ReuseThread"
        $Runspace.Open()
        $Runspace.SessionStateProxy.SetVariable("SyncHash",$SyncHash)
        $Runspace.SessionStateProxy.SetVariable("Code",$Code)
        $Runspace.SessionStateProxy.SetVariable("Parameters",$Parameters)
        $Runspace.SessionStateProxy.SetVariable("Runspace",$Runspace)
        $Runspace.SessionStateProxy.SetVariable("Log",$Log)
        $Runspace.SessionStateProxy.SetVariable("Root",$Root)
        $Runspace.SessionStateProxy.SetVariable("SMBInstances",$global:SMBInstances)
        $Runspace.SessionStateProxy.SetVariable("InstanceId",$SyncHash.InstanceId)
        $SyncHash.Root = $Root
        $SyncHash.Log = $Log
        [scriptblock] $_Code = {}
        <# if($GUI){
                    $_Code = {
                        try{
                            $Code|out-file c:\temp\invoke-operation_GUI.txt
                            $SyncHash.GUI.Dispatcher.invoke(
                                "Render",
                                [action]$Code
                            )
                            $SyncHash.GUI.Dispatcher.invoke(
                                "Render",
                                {}
                            )
                        } catch {
                            
                        }
                    }
                } else { #>

        $_Code = $Code
        # }

        $PSinstance = [powershell]::Create()
        $null = $PSInstance.AddScript({
                $ErrorActionPreference = "Stop"
                $global:Root = $global:SMBInstances[$InstanceId].Root
                $global:SMBInstances = $SMBInstances
    
                foreach($Item in (get-childitem -Path "$($SyncHash.Root)\functions" -Include "*.ps1" -Recurse -Force)){
                    . $Item.FullName
                }
                <# if(($Log -ne $null) -and ((test-path $Log) -ne $false)){
             
                } else {
                    $Log = Start-Log -InstanceId $InstanceId
                }
                $PSDefaultParameterValues = @{"Write-Log:Log"="$Log"} #>

                Register-Classes
                Set-ModuleVariable
                
                $Log = $global:SMBInstances[$InstanceId].Log
                $global:SMBInstances[$InstanceId].Error = $Error
                $PSDefaultParameterValues = @{"Write-Log:Log"=$Log}
                    
            }
        )
        $null = $PSInstance.AddScript($_Code)
        $null = $PSInstance.AddScript({
                $RunSpace.Close()
                $Runspace.Dispose()
            }
                    
        )
        $PSInstance.Runspace = $Runspace
     
        $job = $null
        if($Wait){
            $job = $PSinstance.Invoke()
        } else {
            $job = $PSinstance.BeginInvoke()
        }
        return $job
    } catch {
        write-log -message "Error while invoking operation: $_" -type Error
            
    }


}