powershell - Enter-PSSession 到自定义端点 : Cmdlet not recognized

标签 powershell scripting powershell-remoting endpoints

我正在尝试在我的公司中设置端点服务器并且正在努力连接到它。为了测试,我在全局模块路径中放置了一个 RcLogUtil 模块
C:\windows\system32\WindowsPowershell\v1.0\Modules\RcLogUtil\
导出函数

'Out-LogToEventLog','New-LogMessage'

该计划是让一组特定的用户只访问那些记录功能。

我创建一个 session 配置:
New-PSSessionConfigurationFile -Path C:\Scripts\LoggerEp.pssc `
            -SessionType RestrictedRemoteServer `
            -LanguageMode FullLanguage `
            -ExecutionPolicy Unrestricted `
            -ModulesToImport 'RcLogUtil' `
            -VisibleFunctions 'Out-LogToEventLog' `
            -VisibleCmdlets 'Split-Path'

注册它:
Register-PSSessionConfiguration -Path C:\Scripts\LoggerEp.pssc `
                            -Name loggerep `
                            -ShowSecurityDescriptorUI 

并在我的本地机器上输入:
[W0216]> Enter-PSSession -ComputerName mka-ps-endpoint -ConfigurationName loggerep

Enter-PSSession : One or more errors occurred processing the module 'RcLogUtil' specified in the InitialSessionState object used to create this runspace. See the ErrorRecords property for a complete list of errors. The first error was: The term 'Split-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + Enter-PSSession -ComputerName mka-ps-endpoint -ConfigurationName loggerep + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (:) [Enter-PSSession], RunspaceOpenModuleLoadException + FullyQualifiedErrorId : ErrorLoadingModulesOnRunspaceOpen



现在最大的问题是.. 为什么 Session 无法找到 Split-Path?或者我如何告诉端点加载那个特定的 cmdlet?
我用 SessionType='Default' 成功地尝试了同样的方法,它可以工作,但是周围有所有的 powershell 困惑。

我真的很感激我能得到的任何帮助,因为我已经坚持了很长一段时间了..
谢谢!

最佳答案

在创建 SessionConfiguration 时,可以选择通过使用 -SessionType Default 和 -ScriptsToProcess 'C:\Scripts\LoggerEpStartup.ps1' 参数来提前禁用每个 cmdlet。

New-PSSessionConfigurationFile -Path C:\Scripts\LoggerEp.pssc `
                -SessionType Default `
                -LanguageMode FullLanguage `
                -ExecutionPolicy Unrestricted `
                -ModulesToImport 'RcLogUtil' `
                -VisibleFunctions 'Out-LogToEventLog' `
                -ScriptsToProcess 'C:\Scripts\LoggerEpStartup.ps1'

C:\Scripts\LoggerEpStartup.ps1:
# Commands needed by PSSession (Also the commands used when 
# creating a RestrictedRemoteServer )
$CmdsToExclude = @(
    'Get-Command'   , 'Out-Default'   ,
    'Exit-PSSession', 'Measure-Object',
    'Select-Object' , 'Get-FormatData'
)

# Hide any other commandlets except the ones needed 
# to create a remote session
Get-Command | Where Visibility -eq 'Public' | ForEach-Object {
    if ( $_.Name -notin $CmdsToExclude ) {
        $_.Visibility = 'Private'
    }
}

但我想避免这种方法,因为它似乎是一种笨拙的解决方法,而不是适当的解决方案。

关于powershell - Enter-PSSession 到自定义端点 : Cmdlet not recognized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18656804/

相关文章:

powershell - 当管道文件时,Import-CSV 返回 null

powershell - 使用不同的凭据运行 powershell 脚本

windows - 批处理文件输入验证 - 确保用户输入了一个整数

powershell - 从客户端计算机修改 Active Directory 客户端 OU

powershell - 如何使用 Powershell 在 Windows 服务器上远程工作获得 *tmux* 体验?

Powershell:脚本 block 中的 & 问题

Powershell 哈希表计数和键属性重载

PowerShell:ForEach-Object 的神秘 -RemainingScripts 参数

PowerShell NuGet - "no match was found for the specified search criteria"、 "unable to find dependent packages"等

linux - 用于下载远程文件并启动的 shell 脚本以及用 PHP 编写的其他脚本