Powershell“;“启动服务”;cmdlet不工作。不';我也不会给主机写任何东西
我将此函数保存到StartWindowsService.ps1文件中。 我确保执行策略是“不受限制的”。我以以下方式运行此文件: 在运行下一行之前,我停止了服务中的“FAService”。以便我的职能部门启动服务 我在Powershell命令提示符下运行了以下行。它不写任何东西,也不启动FASservice。我真的很笨Powershell“;“启动服务”;cmdlet不工作。不';我也不会给主机写任何东西,powershell,Powershell,我将此函数保存到StartWindowsService.ps1文件中。 我确保执行策略是“不受限制的”。我以以下方式运行此文件: 在运行下一行之前,我停止了服务中的“FAService”。以便我的职能部门启动服务 我在Powershell命令提示符下运行了以下行。它不写任何东西,也不启动FASservice。我真的很笨 C:\LocationofthisFile\ .\StartWindowsService.ps1 StartWindowsService FAService 我也试过了 fu
C:\LocationofthisFile\ .\StartWindowsService.ps1 StartWindowsService FAService
我也试过了
function StartWindowsService{
Param([string] $ServiceName)
$aService = Get-Service -Name $ServiceName
if ($aService.Status -ne "Running"){
Start-Service $ServiceName
Write-Host "Starting " $ServiceName " service"
" ---------------------- "
" Service is now started"
}
if ($aService.Status -eq "running"){
Write-Host "$ServiceName service is already started"
}
}
谢谢如果您的脚本中只有一个函数,那么当您运行该脚本时,它将启动一个新的PowerShell作用域,定义该函数,因为这就是脚本的全部功能,然后退出并清除它。其他作为参数传递的内容(函数名、服务名)都不会出现,因为脚本没有查找它们。只有函数会这样做,而您不会调用该函数 一种方法是
点源代码脚本,它是\thing.ps1
开头有一个点和一个空格。或者,导入模块。\thing.ps1
。这些将定义函数并将其保留在当前范围内,因此您可以在shell中调用它:
c:\path\ > . .\StartWindowsService.ps1
c:\path\ > StartWindowsService FAService
另一种方法是通过删除定义使脚本成为函数:
Param([string] $ServiceName)
$aService = Get-Service -Name $ServiceName
然后您可以直接从文件中使用它
c:\path\ > .\StartWindowsService.ps1 FAService
该参数进入脚本的Param()
部分,它的工作方式就好像它是一个函数一样