首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自CSV的Powershell读取主机输入

来自CSV的Powershell读取主机输入
EN

Stack Overflow用户
提问于 2017-06-28 22:03:59
回答 1查看 1.1K关注 0票数 0

我正在尝试让这个脚本工作,而不是手动输入sysname,我可以将服务器放入csv并编写脚本,然后输出结果

它只是停留在提示符处等待手动输入

代码语言:javascript
复制
$csvpath = E:\Tsm.csv
$SvcName = '*tsm*scheduler*'
$dataset = import-csv -path $csvpath
$row = ($dataset | where{$_.hostname -eq $SysName})
$SysName = Read-Host -prompt "Enter the target computer name: "
$Tsm = Get-Service -ComputerName $SysName | Where {$_.name -Like $SvcName}
Write-Host "Service :" $Tsm.DisplayName
Write-Host "Status :" $Tsm.Status
Write-host "Start Type :" $Tsm.StartType

If ($Tsm.StartType -ne 'Automatic')
{
    Write-Host "Setting service startup type to Automatic."
    Set-Service -InputObject $Tsm -StartupType Automatic
}
If ($Tsm.Status -ne 'Running')
{
    Write-Host "Starting the service."
    Start-Service -InputObject $Tsm
}

$Tsm2 = Get-Service -ComputerName $SysName | Where {$_.name -Like $SvcName}
Write-Host "Service :" $Tsm2.DisplayName
Write-Host "Status :" $Tsm2.Status
Write-host "Start Type :" $Tsm2.StartType
Export-Csv C:\TestOutput.csv$csvpath = E:\Tsm.csv
EN

回答 1

Stack Overflow用户

发布于 2017-06-28 22:45:42

有很多方法可以得到你想要的东西。基本上,您不应该使用Read-Host,或者只在需要提示和手动等待时才使用它。

有几点:

代码语言:javascript
复制
# this line uses the $SysName variable, which is asked for in the next line. 
# so it will not work correctly.
$row = ($dataset | where{$_.hostname -eq $SysName})       

# if you do not want the waiting and pause on screen, remove this line.
# That's the standard way Read-Host works.
$SysName = Read-Host -prompt "Enter the target computer name: "

一种可能的解决方案是:

代码语言:javascript
复制
param(
    [switch]$manual
)

if($manual){
    $SysName = Read-Host -prompt "Enter the target computer name: "
}else{
    $SysName = "value or variable"
}

使用此解决方案,您可以使用.\script.ps1调用脚本,或使用.\script.ps1 -manual调用Read-Host

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44804779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档