【发布时间】:2015-11-26 02:18:23
【问题描述】:
请原谅我。
我对 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
这是解决此问题的绝望尝试,因为用户期待明天的演示。
【问题讨论】:
-
尝试以有权访问给定路径的其他用户身份运行您的脚本。看看这个:stackoverflow.com/questions/17569678/…
-
访问
\c$共享需要计算机上本地管理员组的成员身份 -
如果当前提供者不是
FileSystem,那么您必须使用提供者名称来限定您的路径:FileSystem::\\$_\c$\windows\system32\logfiles\W3SVC1。 -
@Harsh,Mathias,PetSerAl,感谢你们所有人的友好回应,但我如何将你们的建议融入到脚本中,Harsh/Mathias?
-
您需要创建另一个 powershell 脚本,该脚本将在具有访问网络驱动器权限的给定用户帐户下调用您的脚本。请参阅我发布的链接。
标签: powershell