在PowerShell脚本中帮助DSQuery行 [英] Help with DSQuery line in a powershell script

查看:82
本文介绍了在PowerShell脚本中帮助DSQuery行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行下面的脚本来检查连接并提取服务信息中的日期。

I am running the script below to check connectivity and pull the days in service info.

powershell脚本效果很好 - 但我使用的是DSQuery cmd:

The powershell script works great - but I am using a DSQuery cmd :

ECHO OFF

C:

CD \

CLS

REM ; RNT SMCKENNA SCRIPT到服务器列表C:\
$
ECHO

ECHO

ECHO

ECHO SCRIPT将列出域控制器命名为C:\ SERVER.TXT

PAUSE

set / P DOM =输入DOMIAN:

DSQUERY SERVER - domain%dom%-O RDN> C:\ SERVER.TXT

ECHO OFF
C:
CD\
CLS
REM;RNT SMCKENNA SCRIPT TO PULL SERVER LIST TO C:\
ECHO
ECHO
ECHO
ECHO SCRIPT WILL PUT LIST DOMAIN CONTROLER NAMES IN C:\SERVERS.TXT
PAUSE
set /P DOM=Enter DOMIAN:
DSQUERY SERVER -domain %dom% -O RDN >C:\SERVERS.TXT

我必须先运行才能创建c:\ servers.txt文件。

That I have to run before to create the c:\servers.txt file.

我尝试使用invoke-command将DSQuery添加到PS脚本中 - 但它不会创建文本文件。

I tried adding the DSQuery to the PS script using invoke-command - but it doesn't create the text file.

尝试使用以下命令创建文本文件来自PS:

Tried using the following to create the text file from PS:

$ dc = [adsi]" LDAP:// ou =域控制器,dc = massdoc,dc = local"

$ dc。 psbase.Children |

格式 - 表格 - 属性名称,-AutoSize | out-file c:\ preservereps.txt

$dc = [adsi]"LDAP://ou=domain controllers,dc=massdoc,dc=local"
$dc.psbase.Children |
Format-Table -Property name, -AutoSize |out-file c:\servereps.txt

但它有标题,"{}" ,以及空白行。

But it has titles, "{}'s, and blank lines in it.

我可以-1)修复DSQuery行或更改上面的脚本以便它可以工作

So can I -1) fix the DSQuery line OR change the script above so it will work

或者可以我拉

#MSG BOX NOTIFICATION

Add-类型-AssemblyName System.Windows.Forms

# MSG BOX NOTIFICATION
Add-Type -AssemblyName System.Windows.Forms

$ Message ='此脚本根据C:\ servers.txt中的列表ping Domian中DC服务器的正常运行时间,列表为保存C:\ UPresults.txt'
$
$ Title ='DC UPTIME SCRIPT RNT VER1'

$ Button = [Windows.Forms.MessageBoxButtons] :: OK

$ Icon = [Windows.Forms.MessageBoxIcon] :: Information

$Message = 'This script pings and gets uptime for DC servers in the Domian based on list in C:\servers.txt, the list is saved C:\UPresults.txt'
$Title = 'DC UPTIME SCRIPT RNT VER1'
$Button = [Windows.Forms.MessageBoxButtons]::OK
$Icon = [Windows.Forms.MessageBoxIcon]::Information

[Windows.Forms.MessageBox] :: Show($ Message,$ Title,$ Button, $ Icon)

[Windows.Forms.MessageBox]::Show($Message,$Title,$Button,$Icon)

Invoke-Command DSQUERY SERVER -ourdomain l%dom%-O RDN> C:\ SERVER.TXT

Invoke-Command DSQUERY SERVER -ourdomain l %dom% -O RDN >C:\SERVERS.TXT

  $ names = Get-Content" C:\ servers.txt"

        @(

     foreach($ name in $ names)

       {

            if(Test-Connection -ComputerName $ name -Count 1 -ErrorAction SilentlyContinue)


    {

            $ wmi = gwmi -class Win32_OperatingSystem - 电脑$ name

      $ LBTime = $ wmi.ConvertToDateTime($ wmi.Lastbootuptime)

       [TimeSpan] $ uptime = New-TimeSpan $ LBTime $(获取日期)

     写入输出" $ name正常运行时间是  $($ uptime.days)天$($ uptime.hours)小时$($ uptime.minutes)分钟$($ uptime.seconds)秒钟< b $ b    

  }

             else {

  &nb属;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;写入输出"$ name没有ping"&b $ b $ n            }
         }¥b $ b   )| Out-file -FilePath" c:\ UPresults.txt"

  $names = Get-Content "C:\servers.txt"
        @(
     foreach ($name in $names)
       {
            if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )
    {
            $wmi = gwmi -class Win32_OperatingSystem -computer $name
      $LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
      [TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
      Write-output "$name Uptime is  $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
    
    }
             else {
                Write-output "$name is not pinging"
            }
         }
   ) | Out-file -FilePath "c:\UPresults.txt"

推荐答案

您可以使用Get-ADComputer获取直接进入ForEach-Object的机器和管道列表。

You can use Get-ADComputer to get a list of machines and pipe that directly into ForEach-Object.


这篇关于在PowerShell脚本中帮助DSQuery行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆