我编写了第一个 cmdlet。
\n\n.\\Scripts\\Modules\\NewShare\\New-Share.psm1
\n\n当我将 .\\Modules 添加到 $env:PSModulePath 并导入模块时,我可以检索 help,但无法执行它。
\n\n\n\n\n$evn:psmodulepath =\n C:\\Users\\bp\\Documents\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\;.\\模块;
\n
当我运行脚本时
\n\nNew-Share -foldername \'C:\\MyShare\' -sharename \'MyShare\'\nRun Code Online (Sandbox Code Playgroud)\n\n我收到以下标准错误,因为该模块不存在。
\n\n\n\n\n术语“New-Share”不被识别为 cmdlet、\n 函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在行:1 字符:10\n + New-Share <<<< -foldername \'C:\\MyShare\' -sharename \'MyShare\'\n + CategoryInfo : ObjectNotFound: (New-Share:String) [ ], CommandNotFoundException\n + ExcellentQualifiedErrorId : CommandNotFoundException
\n
问题是什么?
\n\n下面是我的 powershell 模块。
\n\nfunction New-Share\n{ \n <#\n .Synopsis\n This function create a new share\n .Description\n This function creates a new share. If the specified folder does not exist, it will be created, and then shared with the specified share name.\n .Example\n New-Share -foldername \'C:\\MyShare\' -sharename \'MyShare\'\n Creates the share with name \'MyShare\' for folder \'C:\\MyShare\'.\n .Parameter foldername\n The folder that needs to be shared. Will be created if it does not exist.\n .Parameter sharename\n The name for the share.\n #Requires PowerShell 2.0\n #>\n [CmdletBinding()] \n Param (\n [Parameter(Position=0, Mandatory=$True)] \n [alias("fn")] \n [string]$foldername, \n [Parameter(Position=1, Mandatory=$True)] \n [alias("sn")]\n [string]$sharename\n ) \n\n if (!(test-path $foldername)) \n { \n new-item $foldername -type Directory \n } \n\n if (!(get-wmiObject Win32_Share -filter \xe2\x80\x9cname=\'$sharename\'\xe2\x80\x9d)) \n { \n $shares = [WMICLASS]\xe2\x80\x9dWIN32_Share\xe2\x80\x9d\n\n if ($shares.Create($foldername, $sharename, 0).ReturnValue -ne 0) \n {\n throw "Failed to create file share \'$sharename\'"\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
如果您从互联网下载该模块,则应将其放置在C:\Windows\System32\WindowsPowerShell\v1.0\Modules
您应该能够在会话中找到它。
Get-Module -ListAvailable
Run Code Online (Sandbox Code Playgroud)然后就可以添加模块了。
Import-Module newshare
Run Code Online (Sandbox Code Playgroud)验证 cmdlet 是否可用
Get-Command -Module newshare
Run Code Online (Sandbox Code Playgroud)发生错误是因为您没有添加模块。
| 归档时间: |
|
| 查看次数: |
26422 次 |
| 最近记录: |