Powershell Remoting无法使用远程cmdlet

时间:2017-01-16 19:02:53

标签: powershell

目标:

我正在尝试编写一个脚本,每天为100个或更多用户自动隐式远程处理。

我正在听Don Jones的这个教程; https://technet.microsoft.com/en-us/library/ff720181.aspx

问题:

输入自定义cmdlet后出现错误,例如:

PS > cmdlet1 FooBar
d : The term 'cmdlet1' 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
+ cmdlet1 FooBar
+ ~
    + CategoryInfo          : ObjectNotFound: (d:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我的剧本:

Set-Alias -Name gm -Value Get-TheModules -Description 'Grabs The Modules'
function Get-TheModules
{ 
  $ServerARM = New-PSSession -ComputerName serverA -Authentication Kerberos
  # Import Modules
  Write-Verbose -Message 'Importing Custom modules...'
  Invoke-command -ScriptBlock { import-module -Name 'Z:\Modules\ModuleA' } -Session $ServerARM
  Invoke-command -ScriptBlock { import-module -Name 'Z:\Modules\ModuleB' } -Session $ServerARM
  Invoke-command -ScriptBlock { import-module -Name 'Z:\Modules\ModuleC' } -Session $ServerARM
  Export-PSSession -Session $ServerARM -commandname *Cmdlet1,Cmdlet2,Cmdlet3* -OutputModule 'CustomCMDlets' -Force -AllowClobber
  Remove-PSSession -Session $ServerARM
  Import-Module CustomMods -prefix CRem
  Write-Verbose -Message 'Custom modules successfully imported...'
}

运行脚本的结果:

PS> GM

    Directory: Z:\Users\-\Documents\WindowsPowerShell\Modules\CustomCMDlets


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/16/2017   1:57 PM             99 CustomCMDlets.format.ps1xml
-a----        1/16/2017   1:57 PM            801 CustomCMDlets.psd1
-a----        1/16/2017   1:57 PM          41584 CustomCMDlets.psm1

Custom modules successfully imported...

问题:

鉴于我的目标和问题,我对这篇文章的理解是什么,我的脚本中哪里有问题?

1 个答案:

答案 0 :(得分:2)

如果您的功能名称为Cmdlet1,但您使用Import-Module -Prefix CRem导入该名称,则导入的功能名称将为CRemCmdlet1

如果函数以正确的Verb-Noun语法命名,则前缀将放在名词之前,因此如果名称为Invoke-Cmdlet1,那么当使用前缀导入时,它将为Invoke-CRemCmdlet1