コード例 #1
0
        private void GenerateCommandProxy(TextWriter writer, CommandMetadata commandMetadata)
        {
            if (writer == null)
            {
                throw PSTraceSource.NewArgumentNullException("writer");
            }
            string str  = CommandMetadata.EscapeSingleQuotedString(commandMetadata.Name);
            string str2 = this.EscapeFunctionNameForRemoteHelp(commandMetadata.Name);

            object[] arg = new object[9];
            arg[0] = str;
            arg[1] = str2;
            arg[2] = commandMetadata.GetDecl();
            arg[3] = commandMetadata.GetParamBlock();
            arg[5] = commandMetadata.WrappedCommandType;
            arg[6] = ProxyCommand.GetProcess(commandMetadata);
            arg[7] = ProxyCommand.GetEnd(commandMetadata);
            arg[8] = commandMetadata.WrappedAnyCmdlet;
            writer.Write("\r\n& $script:SetItem 'function:script:{0}' `\r\n{{\r\n    param(\r\n    {3})\r\n\r\n    Begin {{\r\n        try {{\r\n            $positionalArguments = & $script:NewObject collections.arraylist\r\n            foreach ($parameterName in $PSBoundParameters.BoundPositionally)\r\n            {{\r\n                $null = $positionalArguments.Add( $PSBoundParameters[$parameterName] )\r\n                $null = $PSBoundParameters.Remove($parameterName)\r\n            }}\r\n            $positionalArguments.AddRange($args)\r\n\r\n            $clientSideParameters = Get-PSImplicitRemotingClientSideParameters $PSBoundParameters ${8}\r\n\r\n            $scriptCmd = {{ & $script:InvokeCommand `\r\n                            @clientSideParameters `\r\n                            -HideComputerName `\r\n                            -Session (Get-PSImplicitRemotingSession -CommandName '{0}') `\r\n                            -Arg ('{0}', $PSBoundParameters, $positionalArguments) `\r\n                            -Script {{ param($name, $boundParams, $unboundParams) & $name @boundParams @unboundParams }} `\r\n                         }}\r\n\r\n            $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)\r\n            $steppablePipeline.Begin($myInvocation.ExpectingInput, $ExecutionContext)\r\n        }} catch {{\r\n            throw\r\n        }}\r\n    }}\r\n    Process {{ {6} }}\r\n    End {{ {7} }}\r\n\r\n    # .ForwardHelpTargetName {1}\r\n    # .ForwardHelpCategory {5}\r\n    # .RemoteHelpRunspace PSSession\r\n}}\r\n        ", arg);
        }
コード例 #2
0
        private string GenerateArrayString(IEnumerable <string> listOfStrings)
        {
            if (listOfStrings == null)
            {
                throw PSTraceSource.NewArgumentNullException("listOfStrings");
            }
            StringBuilder builder = new StringBuilder();

            foreach (string str in listOfStrings)
            {
                if (builder.Length != 0)
                {
                    builder.Append(", ");
                }
                builder.Append('\'');
                builder.Append(CommandMetadata.EscapeSingleQuotedString(str));
                builder.Append('\'');
            }
            builder.Insert(0, "@(");
            builder.Append(")");
            return(builder.ToString());
        }
コード例 #3
0
        private void GenerateAliases(TextWriter writer, Dictionary <string, string> alias2resolvedCommandName)
        {
            this.GenerateSectionSeparator(writer);
            foreach (KeyValuePair <string, string> pair in alias2resolvedCommandName)
            {
                string key           = pair.Key;
                string stringContent = pair.Value;
                writer.Write("\r\n& $script:SetAlias -Name '{0}' -Value '{1}' -Force -Scope script\r\n        ", CommandMetadata.EscapeSingleQuotedString(key), CommandMetadata.EscapeSingleQuotedString(stringContent));
            }
            string str3 = this.GenerateArrayString(alias2resolvedCommandName.Keys);

            writer.Write("\r\n& $script:ExportModuleMember -Alias {0}\r\n        ", str3);
        }
コード例 #4
0
        private string GenerateReimportingOfModules()
        {
            StringBuilder builder = new StringBuilder();

            if (this.invocationInfo.BoundParameters.ContainsKey("Module"))
            {
                string[] strArray = (string[])this.invocationInfo.BoundParameters["Module"];
                foreach (string str in strArray)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, "\r\n            try {{\r\n                & $script:InvokeCommand -Session $script:PSSession -ScriptBlock {{ \r\n                    Get-Module -ListAvailable -Name '{0}' | Import-Module \r\n                }} -ErrorAction SilentlyContinue\r\n            }} catch {{ }}\r\n", new object[] { CommandMetadata.EscapeSingleQuotedString(str) });
                }
            }
            return(builder.ToString());
        }
コード例 #5
0
 private string GenerateProxyCredentialParameter(WSManConnectionInfo wsmanConnectionInfo)
 {
     if ((wsmanConnectionInfo == null) || (wsmanConnectionInfo.ProxyCredential == null))
     {
         return(string.Empty);
     }
     return(string.Format(CultureInfo.InvariantCulture, "-ProxyCredential ( $host.UI.PromptForCredential( '{0}', '{1}', '{2}', '{3}' ) ) ", new object[] { CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.CredentialRequestTitle, new object[0])), CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.ProxyCredentialRequestBody, this.GetConnectionString())), CommandMetadata.EscapeSingleQuotedString(wsmanConnectionInfo.ProxyCredential.UserName), CommandMetadata.EscapeSingleQuotedString(this.remoteRunspaceInfo.ComputerName + @"\httpproxy") }));
 }
コード例 #6
0
 private string GenerateNewRunspaceExpression()
 {
     return(string.Format(CultureInfo.InvariantCulture, "\r\n            $( \r\n                & $script:NewPSSession `\r\n                    {0} -ConfigurationName '{1}' `\r\n                    -SessionOption (Get-PSImplicitRemotingSessionOption) `\r\n                    {2} `\r\n                    {3} `\r\n                    {4} `\r\n                    {5} `\r\n            )\r\n", new object[] { this.GenerateConnectionStringForNewRunspace(), CommandMetadata.EscapeSingleQuotedString(this.remoteRunspaceInfo.ConfigurationName), this.GenerateCredentialParameter(), this.GenerateCertificateThumbprintParameter(), this.GenerateAuthenticationMechanismParameter(), this.GenerateAllowRedirectionParameter() }));
 }
コード例 #7
0
        private string GenerateNewPSSessionOption()
        {
            StringBuilder          builder        = new StringBuilder("& $script:NewPSSessionOption ");
            RunspaceConnectionInfo connectionInfo = this.remoteRunspaceInfo.Runspace.ConnectionInfo;

            if (connectionInfo != null)
            {
                builder.AppendFormat(null, "-Culture '{0}' ", new object[] { CommandMetadata.EscapeSingleQuotedString(connectionInfo.Culture.ToString()) });
                builder.AppendFormat(null, "-UICulture '{0}' ", new object[] { CommandMetadata.EscapeSingleQuotedString(connectionInfo.UICulture.ToString()) });
                builder.AppendFormat(null, "-CancelTimeOut {0} ", new object[] { connectionInfo.CancelTimeout });
                builder.AppendFormat(null, "-IdleTimeOut {0} ", new object[] { connectionInfo.IdleTimeout });
                builder.AppendFormat(null, "-OpenTimeOut {0} ", new object[] { connectionInfo.OpenTimeout });
                builder.AppendFormat(null, "-OperationTimeOut {0} ", new object[] { connectionInfo.OperationTimeout });
            }
            WSManConnectionInfo wsmanConnectionInfo = this.remoteRunspaceInfo.Runspace.ConnectionInfo as WSManConnectionInfo;

            if (wsmanConnectionInfo != null)
            {
                if (!wsmanConnectionInfo.UseCompression)
                {
                    builder.Append("-NoCompression ");
                }
                if (wsmanConnectionInfo.NoEncryption)
                {
                    builder.Append("-NoEncryption ");
                }
                if (wsmanConnectionInfo.NoMachineProfile)
                {
                    builder.Append("-NoMachineProfile ");
                }
                if (wsmanConnectionInfo.UseUTF16)
                {
                    builder.Append("-UseUTF16 ");
                }
                if (wsmanConnectionInfo.SkipCACheck)
                {
                    builder.Append("-SkipCACheck ");
                }
                if (wsmanConnectionInfo.SkipCNCheck)
                {
                    builder.Append("-SkipCNCheck ");
                }
                if (wsmanConnectionInfo.SkipRevocationCheck)
                {
                    builder.Append("-SkipRevocationCheck ");
                }
                if (wsmanConnectionInfo.MaximumReceivedDataSizePerCommand.HasValue)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, "-MaximumReceivedDataSizePerCommand {0} ", new object[] { wsmanConnectionInfo.MaximumReceivedDataSizePerCommand.Value });
                }
                if (wsmanConnectionInfo.MaximumReceivedObjectSize.HasValue)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, "-MaximumReceivedObjectSize {0} ", new object[] { wsmanConnectionInfo.MaximumReceivedObjectSize.Value });
                }
                builder.AppendFormat(CultureInfo.InvariantCulture, "-MaximumRedirection {0} ", new object[] { wsmanConnectionInfo.MaximumConnectionRedirectionCount });
                builder.AppendFormat(CultureInfo.InvariantCulture, "-ProxyAccessType {0} ", new object[] { wsmanConnectionInfo.ProxyAccessType.ToString() });
                builder.AppendFormat(CultureInfo.InvariantCulture, "-ProxyAuthentication {0} ", new object[] { wsmanConnectionInfo.ProxyAuthentication.ToString() });
                builder.Append(this.GenerateProxyCredentialParameter(wsmanConnectionInfo));
            }
            if (this.GetApplicationArguments() != null)
            {
                builder.Append("-ApplicationArguments $(");
                builder.Append("& $script:ImportCliXml -Path $(");
                builder.Append("& $script:JoinPath -Path $PSScriptRoot -ChildPath ApplicationArguments.xml");
                builder.Append(")");
                builder.Append(") ");
            }
            return(builder.ToString());
        }
コード例 #8
0
        private void GenerateModuleHeader(TextWriter writer)
        {
            if (writer == null)
            {
                throw PSTraceSource.NewArgumentNullException("writer");
            }
            string str = "[" + typeof(ExportPSSessionCommand).AssemblyQualifiedName + "]::VersionOfScriptGenerator";

            this.GenerateTopComment(writer);
            writer.Write("\r\nparam(\r\n    <# {0} #>    \r\n    [System.Management.Automation.Runspaces.PSSession] $PSSessionOverride,\r\n    [System.Management.Automation.Remoting.PSSessionOption] $PSSessionOptionOverride\r\n)\r\n\r\n$script:__psImplicitRemoting_versionOfScriptGenerator = {1}\r\nif ($script:__psImplicitRemoting_versionOfScriptGenerator.Major -ne {2})\r\n{{\r\n    throw '{3}'\r\n}}\r\n\r\n\r\n$script:WriteHost = $executionContext.InvokeCommand.GetCommand('Write-Host', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:WriteWarning = $executionContext.InvokeCommand.GetCommand('Write-Warning', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:GetPSSession = $executionContext.InvokeCommand.GetCommand('Get-PSSession', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:NewPSSession = $executionContext.InvokeCommand.GetCommand('New-PSSession', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:ConnectPSSession = $executionContext.InvokeCommand.GetCommand('Connect-PSSession', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:NewObject = $executionContext.InvokeCommand.GetCommand('New-Object', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:RemovePSSession = $executionContext.InvokeCommand.GetCommand('Remove-PSSession', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:InvokeCommand = $executionContext.InvokeCommand.GetCommand('Invoke-Command', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:SetItem = $executionContext.InvokeCommand.GetCommand('Set-Item', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:ImportCliXml = $executionContext.InvokeCommand.GetCommand('Import-CliXml', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:NewPSSessionOption = $executionContext.InvokeCommand.GetCommand('New-PSSessionOption', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:JoinPath = $executionContext.InvokeCommand.GetCommand('Join-Path', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:ExportModuleMember = $executionContext.InvokeCommand.GetCommand('Export-ModuleMember', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n$script:SetAlias = $executionContext.InvokeCommand.GetCommand('Set-Alias', [System.Management.Automation.CommandTypes]::Cmdlet)\r\n\r\n$script:MyModule = $MyInvocation.MyCommand.ScriptBlock.Module\r\n        ", new object[] { CommandMetadata.EscapeBlockComment(StringUtil.Format(ImplicitRemotingStrings.ModuleHeaderRunspaceOverrideParameter, new object[0])), str, VersionOfScriptWriter, CommandMetadata.EscapeSingleQuotedString(string.Format(null, PathUtilsStrings.ExportPSSession_ScriptGeneratorVersionMismatch, new object[] { "Export-PSSession" })) });
        }
コード例 #9
0
 private void GenerateManifest(TextWriter writer, string psm1fileName, string formatPs1xmlFileName)
 {
     if (writer == null)
     {
         throw PSTraceSource.NewArgumentNullException("writer");
     }
     this.GenerateTopComment(writer);
     writer.Write("\r\n@{{\r\n    GUID = '{0}'\r\n    Description = '{1}'\r\n    ModuleToProcess = @('{2}')\r\n    FormatsToProcess = @('{3}')\r\n\r\n    ModuleVersion = '1.0'\r\n\r\n    PrivateData = @{{\r\n        ImplicitRemoting = $true\r\n    }}\r\n}}\r\n        ", new object[] { CommandMetadata.EscapeSingleQuotedString(this.moduleGuid.ToString()), CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.ProxyModuleDescription, this.GetConnectionString())), CommandMetadata.EscapeSingleQuotedString(Path.GetFileName(psm1fileName)), CommandMetadata.EscapeSingleQuotedString(Path.GetFileName(formatPs1xmlFileName)) });
 }
コード例 #10
0
        private void GenerateHelperFunctionsSetImplicitRunspace(TextWriter writer)
        {
            if (writer == null)
            {
                throw PSTraceSource.NewArgumentNullException("writer");
            }
            string stringContent = StringUtil.Format(ImplicitRemotingStrings.ProxyRunspaceNameTemplate, new object[0]);

            writer.Write("\r\n$script:PSSession = $null\r\n\r\nfunction Get-PSImplicitRemotingModuleName {{ $myInvocation.MyCommand.ScriptBlock.File }}\r\n\r\nfunction Set-PSImplicitRemotingSession\r\n{{\r\n    param(\r\n        [Parameter(Mandatory = $true, Position = 0)]\r\n        [AllowNull()]\r\n        [Management.Automation.Runspaces.PSSession] \r\n        $PSSession, \r\n\r\n        [Parameter(Mandatory = $false, Position = 1)]\r\n        [bool] $createdByModule = $false)\r\n\r\n    if ($PSSession -ne $null)\r\n    {{\r\n        $script:PSSession = $PSSession\r\n\r\n        if ($createdByModule -and ($script:PSSession -ne $null))\r\n        {{\r\n            $moduleName = Get-PSImplicitRemotingModuleName \r\n            $script:PSSession.Name = '{0}' -f $moduleName\r\n            \r\n            $oldCleanUpScript = $script:MyModule.OnRemove\r\n            $removePSSessionCommand = $script:RemovePSSession\r\n            $script:MyModule.OnRemove = {{ \r\n                & $removePSSessionCommand -Session $PSSession -ErrorAction SilentlyContinue\r\n                if ($oldCleanUpScript)\r\n                {{\r\n                    & $oldCleanUpScript $args\r\n                }}\r\n            }}.GetNewClosure()\r\n        }}\r\n    }}\r\n}}\r\n\r\nif ($PSSessionOverride) {{ Set-PSImplicitRemotingSession $PSSessionOverride }}\r\n", CommandMetadata.EscapeSingleQuotedString(stringContent));
        }
コード例 #11
0
        private void GenerateHelperFunctionsGetImplicitRunspace(TextWriter writer)
        {
            string str;

            if (writer == null)
            {
                throw PSTraceSource.NewArgumentNullException("writer");
            }
            PSPrimitiveDictionary.TryPathGet <string>(this.remoteRunspaceInfo.ApplicationPrivateData, out str, new string[] { "ImplicitRemoting", "Hash" });
            str = str ?? string.Empty;
            writer.Write("\r\nfunction Get-PSImplicitRemotingSession\r\n{{\r\n    param(\r\n        [Parameter(Mandatory = $true, Position = 0)]\r\n        [string] \r\n        $commandName\r\n    )\r\n\r\n    $savedImplicitRemotingHash = '{4}'\r\n\r\n    if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))\r\n    {{\r\n        Set-PSImplicitRemotingSession `\r\n            (& $script:GetPSSession `\r\n                -InstanceId {0} `\r\n                -ErrorAction SilentlyContinue )\r\n    }}\r\n    if (($script:PSSession -ne $null) -and ($script:PSSession.Runspace.RunspaceStateInfo.State -eq 'Disconnected'))\r\n    {{\r\n        # If we are handed a disconnected session, try re-connecting it before creating a new session.\r\n        Set-PSImplicitRemotingSession `\r\n            (& $script:ConnectPSSession `\r\n                -Session $script:PSSession `\r\n                -ErrorAction SilentlyContinue)\r\n    }}\r\n    if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))\r\n    {{\r\n        Write-PSImplicitRemotingMessage ('{1}' -f $commandName)\r\n\r\n        Set-PSImplicitRemotingSession `\r\n            -CreatedByModule $true `\r\n            -PSSession ( {2} )\r\n\r\n        if ($savedImplicitRemotingHash -ne '')\r\n        {{\r\n            $newImplicitRemotingHash = [string]($script:PSSession.ApplicationPrivateData.{6}.{7})\r\n            if ($newImplicitRemotingHash -ne $savedImplicitRemotingHash)\r\n            {{\r\n                & $script:WriteWarning -Message '{5}'\r\n            }}\r\n        }}\r\n\r\n        {8}\r\n    }}\r\n    if (($script:PSSession -eq $null) -or ($script:PSSession.Runspace.RunspaceStateInfo.State -ne 'Opened'))\r\n    {{\r\n        throw '{3}'\r\n    }}\r\n    return [Management.Automation.Runspaces.PSSession]$script:PSSession\r\n}}\r\n", new object[] { this.remoteRunspaceInfo.InstanceId, CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.CreateNewRunspaceMessageTemplate, new object[0])), this.GenerateNewRunspaceExpression(), CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.ErrorNoRunspaceForThisModule, new object[0])), CommandMetadata.EscapeSingleQuotedString(str), CommandMetadata.EscapeSingleQuotedString(StringUtil.Format(ImplicitRemotingStrings.WarningMismatchedImplicitRemotingHash, new object[0])), "ImplicitRemoting", "Hash", this.GenerateReimportingOfModules() });
        }
コード例 #12
0
        private string GenerateConnectionStringForNewRunspace()
        {
            string str = null;
            WSManConnectionInfo connectionInfo = this.remoteRunspaceInfo.Runspace.ConnectionInfo as WSManConnectionInfo;

            if (connectionInfo == null)
            {
                return(str);
            }
            if (connectionInfo.UseDefaultWSManPort)
            {
                bool flag;
                WSManConnectionInfo.GetConnectionString(connectionInfo.ConnectionUri, out flag);
                return(string.Format(CultureInfo.InvariantCulture, "-ComputerName '{0}' `\r\n                    -ApplicationName '{1}' {2} {3} ", new object[] { CommandMetadata.EscapeSingleQuotedString(connectionInfo.ComputerName), CommandMetadata.EscapeSingleQuotedString(connectionInfo.AppName), connectionInfo.UseDefaultWSManPort ? string.Empty : string.Format(CultureInfo.InvariantCulture, "-Port {0} ", new object[] { connectionInfo.Port }), flag ? "-useSSL" : string.Empty }));
            }
            return(string.Format(CultureInfo.InvariantCulture, "-connectionUri '{0}'", new object[] { CommandMetadata.EscapeSingleQuotedString(this.GetConnectionString()) }));
        }
コード例 #13
0
 private string GenerateCertificateThumbprintParameter()
 {
     if (this.remoteRunspaceInfo.Runspace.ConnectionInfo.CertificateThumbprint == null)
     {
         return(string.Empty);
     }
     return(string.Format(CultureInfo.InvariantCulture, "-CertificateThumbprint '{0}'", new object[] { CommandMetadata.EscapeSingleQuotedString(this.remoteRunspaceInfo.Runspace.ConnectionInfo.CertificateThumbprint) }));
 }