我写了我的第一个cmdlet。
\脚本\Modules\NewShare\New-Share.psm1
当我将.\Modules添加到$env:PSModulePath并导入模块时,就可以检索帮助,但是我无法执行它。
C:\Users\bp\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;.\Modules;= $evn:psmodulepath
当我运行脚本时
New-Share -foldername 'C:\MyShare' -sharename 'MyShare'我得到以下标准错误,因为模块不存在。
术语“新共享”不被识别为cmdlet、函数、脚本文件或可操作程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后再试一次。一行:1字符:10+新股<<<< -foldername 'C:\MyShare‘-sharename 'MyShare’+ CategoryInfo : ObjectNotFound:(新股票:String) [],CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
有什么问题吗?
下面是我的powershell模块。
function New-Share
{
<#
.Synopsis
This function create a new share
.Description
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.
.Example
New-Share -foldername 'C:\MyShare' -sharename 'MyShare'
Creates the share with name 'MyShare' for folder 'C:\MyShare'.
.Parameter foldername
The folder that needs to be shared. Will be created if it does not exist.
.Parameter sharename
The name for the share.
#Requires PowerShell 2.0
#>
[CmdletBinding()]
Param (
[Parameter(Position=0, Mandatory=$True)]
[alias("fn")]
[string]$foldername,
[Parameter(Position=1, Mandatory=$True)]
[alias("sn")]
[string]$sharename
)
if (!(test-path $foldername))
{
new-item $foldername -type Directory
}
if (!(get-wmiObject Win32_Share -filter “name='$sharename'”))
{
$shares = [WMICLASS]”WIN32_Share”
if ($shares.Create($foldername, $sharename, 0).ReturnValue -ne 0)
{
throw "Failed to create file share '$sharename'"
}
}
}发布于 2015-03-22 11:18:41
如果您从互联网上下载了该模块,则应该将其放在C:\Windows\System32\WindowsPowerShell\v1.0\Modules中。
发生错误是因为您没有添加模块。
https://stackoverflow.com/questions/29193547
复制相似问题