知道如何解决这个PowerShell错误吗?

时间:2015-11-24 22:17:54

标签: powershell

请原谅我这个。

我对Power shell非常陌生。

我昨天在这里发了一个问题,要求以编程方式协助阅读iis日志的内容。

尚无回应。

因此,经过艰苦的谷歌搜索,我发现了一个出现的Power Shell脚本,接近我正在寻找的内容。

当我尝试执行它时,我收到以下错误:

Get-ChildItem : Cannot find path '\\servername\c$\windows\system32\logfiles\
W3SVC1' because it does not exist.

我有理由相信这更多是权限问题,因为路径存在。

有谁知道如何在下面的脚本中添加用户名和密码?

提前多多感谢。

#
# Script constants
#

$Server = 'myServer'
$Days = 1

Function Read-IISLog {
  [CmdLetBinding()]
  Param(
    [Parameter(ValueFromPipelineByPropertyName = $True)]
      [ValidateScript( { $_ | %{ Test-Path $_ } } )]
    [Alias("FullName")]
    [String[]]$Path
  )

  Process {

    $Path | ForEach-Object {
      $FullPath = (Get-Item $_).FullName
      $FileStream = New-Object IO.FileStream($FullPath, "Open", "Read", "ReadWrite")
      $StreamReader = New-Object IO.StreamReader($FileStream)

      For ($i = 0; $i -lt 4; $i++) {
        $Line = $StreamReader.ReadLine()
        If ($Line -Match '#Fields: ') {
          $Header = ($Line -Replace '#Fields: ').Split(' ', [StringSplitOptions]::RemoveEmptyEntries)
        }
      }
      Do {
        $Line = $StreamReader.ReadLine()
        If ($Line -NotLike "#*") {
          $Line | ConvertFrom-Csv -Delimiter ' ' -Header $Header
        }
      } Until ($StreamReader.EndOfStream)
    }

  }
}

$Output = @{}

$Server | ForEach-Object {
  Get-ChildItem "\\$_\c$\windows\system32\logfiles\W3SVC1" -Filter *.log | 
    Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-$Days) } |
    Read-IISLog |
    Where-Object { $_."cs-uri-stem" -Match 'ActiveSync' } 
} | Select-Object `
    @{n='FirstSync';e={ [DateTime]::ParseExact("$($_.date) $($_.time)", "yyyy-MM-dd HH:mm:ss", $Null) }},
    @{n='LastSync';e={ [DateTime]::ParseExact("$($_.date) $($_.time)", "yyyy-MM-dd HH:mm:ss", $Null) }},
    @{n='ClientIP';e={ [Net.IPAddress]($_."c-ip") }},
    @{n='ClientDevice';e={ $_."cs(User-Agent)" }},
    @{n='AuthenticatedUser';e={ $_."cs-username" }},
    @{n='Mailbox';e={ $_."cs-uri-stem".Split("/")[2] }},
    @{n='DeviceId';e={ $_."cs-uri-stem".Split("/")[-1] }},
    @{n='DeviceType';e={ $_."cs-uri-stem".Split("/")[-2] }},
    @{n='SyncCount';e={ 1 }} |
  ForEach-Object {
    If (!$Output.Contains($_.DeviceId)) {
      $Output.Add($_.DeviceId, $_)
    } Else {
      If ($_.FirstSync -lt $Output[$_.DeviceId].FirstSync) { $Output[$_.DeviceId].FirstSync = $_.FirstSync }
      If ($_.LastSync -gt $Output[$_.DeviceId].LastSync)   { $Output[$_.DeviceId].LastSync = $_.LastSync }
      $Output[$_.DeviceId].SyncCount += 1
    }
  }
$Output.Values

这是一个绝望的尝试来解决这个问题,因为用户期待明天的演示。

1 个答案:

答案 0 :(得分:0)

谢谢你们的帮助。

我找到了一个更好的解决方案,我可以更好地管理它 - LogParser:

https://mlichtenberg.wordpress.com/2011/02/03/log-parser-rocks-more-than-50-examples/