在ForEach中使用添加内容

时间:2018-11-10 05:20:02

标签: powershell

我正在寻找运行Powershell脚本的脚本,该脚本将创建另一个.ps1,以将服务启动类型恢复为最初的状态。即。 SQL Server代理设置为自动,因此将其重新设置为自动。由于我需要对远程服务器执行此操作,因此这使其变得更加复杂。这是我微不足道的开始:

$SQLServices = Get-Service -ComputerName 'enter servername here' | Select-Object Name,DisplayName,Status,StartType | ?{$_.Displayname -match “SQL Server Agent” -AND $_.Status -eq "Running"}

ForEach {Add-Content -path 'c:\temp\RestartSQLServices.ps1' -value "Get-Service -ComputerName 'enter servername here' | Set-Service -Name $($_.Name) -StartupType $($_.StartType)"}

新的ps1应该显示为:

Get-Service -ComputerName 'enter servername here' | Set-Service -Name SQLSERVERAGENT -StartupType Automatic

但是我似乎无法从$ SQLServices中读取服务名称和StartType。

想法?

1 个答案:

答案 0 :(得分:0)

为什么要尝试创建另一个脚本?

只需检查服务并将其重置,您确定使用正确的服务名称即可。

Get-Service -ComputerName 'LabSQL01' -Name '*SQL*'

# Results

Status   Name               DisplayName                           
------   ----               -----------                           
Running  MSSQLFDLauncher    SQL Full-text Filter Daemon Launche...
Running  MSSQLSERVER        SQL Server (MSSQLSERVER)              
Running  SQLBrowser         SQL Server Browser                    
Running  SQLSERVERAGENT     SQL Server Agent (MSSQLSERVER)        
Running  SQLTELEMETRY       SQL Server CEIP service (MSSQLSERVER) 
Running  SQLWriter          SQL Server VSS Writer 


($SQLServiceStatus = Get-Service -ComputerName 'LabSQL01' -Name 'SQL Server Agent (MSSQLSERVER)')

$SQLServiceStatus.StartType
Automatic

$SQLServiceStatus.Status
Running     

{Invoke-Command -ComputerName 'LabSQO01' -ScriptBlock {$SQLServiceStatus = Get-Service -ComputerName $env:COMPUTERNAME -Name 'SQL Server Agent (MSSQLSERVER)'

If ($SQLServiceStatus.Status -eq 'Running')
{Set-Service -Name $($SQLServiceStatus.Name) -StartupType Automatic -WhatIf}
Else{ Write-Warning -Message "$($SQLServiceStatus.Name) is already set to the current state" }}

* OP更新*

那么,我不确定为什么为什么要在一台计算机上使用forloop,除非您打算将多个服务器作为目标,但是对于一个目标,该怎么做。

New-Item -Path  "\\$LabSQL01\c$\temp" -Name 'RestartSQLServices.ps1' -ItemType File

# Results

    Directory: \\LabSQL01\c$\temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        11/10/2018   3:39 PM          0 RestartSQLServices.ps1


Get-ChildItem -Path "\\$LabSQL01\c$\temp\RestartSQLServices.ps1"

# Results

    Directory: \\LabSQL01\c$\temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        11/10/2018   3:39 PM          0 RestartSQLServices.ps1


$ScriptContents = @'
$SQLServiceStatus = Get-Service -ComputerName $env:COMPUTERNAME -Name 'SQL Server Agent (MSSQLSERVER)'

If ($SQLServiceStatus.Status -eq 'Running')
{Set-Service -Name $($SQLServiceStatus.Name) -StartupType Automatic -WhatIf}
Else{ Write-Warning -Message "$($SQLServiceStatus.Name) is already set to the current state" }
'@

Add-Content -Path "\\$LabSQL01\c$\temp\RestartSQLServices.ps1" -Value $ScriptContents


Get-Content -Path "\\$LabSQL01\c$\temp\RestartSQLServices.ps1"

# Results

$SQLServiceStatus = Get-Service -ComputerName $env:COMPUTERNAME -Name 'SQL Server Agent (MSSQLSERVER)'

If ($SQLServiceStatus.Status -eq 'Running')
{Set-Service -Name $($SQLServiceStatus.Name) -StartupType Automatic -WhatIf}
Else{ Write-Warning -Message "$($SQLServiceStatus.Name) is already set to the current state" }


Get-ChildItem -Path "\\$LabSQL01\c$\temp\RestartSQLServices.ps1"

# Results

    Directory: \\LabSQL01\c$\temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        11/10/2018   3:45 PM        273 RestartSQLServices.ps1  

当然可以轻松地针对多个目标进行调整。