简体   繁体   English

从登录脚本启动的Powershell脚本不起作用

[英]Powershell script launched from logon script not working

I'm trying to create a powershell script that searches a users C drive for a certain file extension, and then writes a file to a network share if it finds one. 我正在尝试创建一个Powershell脚本,该脚本在用户C驱动器中搜索某个文件扩展名,然后在找到共享文件时将其写入网络共享。 The script is launched from the last line of a logon script that reads like this: 该脚本从登录脚本的最后一行启动,其内容如下:

powershell.exe -ExecutionPolicy Bypass -windowstyle hidden -file "\\\\servername\\Path\\To\\File.ps1"

And my powershell script looks like this: 我的powershell脚本如下所示:

$hostname = HostName

Get-ChildItem -Path C:\*.* -Filter $file -Recurse -Force -ErrorAction 
SilentlyContinue | Out-File \\servername\Path\To\Results\$hostname\file.txt

If ((Get-Content "\\servername\Path\To\Results\$hostname\file.txt") -eq $Null) {

    Remove-Item \\servername\Path\To\Results\$hostname\file.txt
}
Exit

The script runs perfectly fine on my machine even when I load it from the network share but whenever another computer runs it, it never produces an Out File. 即使从网络共享中加载脚本,该脚本在我的计算机上也可以很好地运行,但是每当另一台计算机运行该脚本时,它都不会产生Out File。 And I don't think it is even searching. 而且我认为它甚至都没有搜索到。

What am I doing wrong? 我究竟做错了什么? I thought it was execution policy, but I've set the logon script to bypass it. 我以为这是执行策略,但是我将登录脚本设置为绕过它。 I don't understand why it isn't working. 我不明白为什么它不起作用。

[edit] I've now got the script working sometimes on Windows 10 machines. [edit]我现在有时可以在Windows 10计算机上使用该脚本。 But it doesn't work at all on Windows 7. I've even tried running 但是它在Windows 7上根本不起作用。我什至尝试运行

Get-ChildItem C:\\*.pst -Recurse

directly from a powershell command prompt, and it just fails silently and doesn't search for anything. 直接从powershell命令提示符下,它只是默默地失败,并且不搜索任何内容。 Isn't Get-ChildItem a powershell 1 command? Get-ChildItem不是powershell 1命令吗?

Hello. 你好。 If you do like this: Get-ChildItem -Path C:*.* -Filter $file -Recurse -Force the text file output will be enough to weigh. 如果您这样做:Get-ChildItem -Path C:*。* -Filter $ file -Recurse -Force文本文件输出将足以称量。 You can try to check the access to the network folder for the current user:if access explicitly set, and write access exists, then you can record a file with the content. 您可以尝试检查当前用户对网络文件夹的访问权限:如果显式设置了访问权限,并且存在写访问权限,则可以记录包含内容的文件。 Otherwise, it can create folder test on the local machine which will create the file, indicating that there is no access. 否则,它可以在将创建该文件的本地计算机上创建文件夹测试,表明没有访问权限。 How is this way: 这是怎么回事:

Set-ExecutionPolicy remotesigned -Scope CurrentUser -Force| Out-Null

$user = [System.Environment]::UserName

$hostname = [System.Environment]::MachineName

try {
$accs = Get-ACL -Path "\\server\sharedfolder\Results\" 

foreach ($access in $accs) {

$obj = New-Object -TypeName PSObject -Property @{

Access = $accs.Access

}

$obj1 = $obj | select -ExpandProperty Access

for ($i = 0 ; $i -le $obj1.Count ; $i ++ )

{

if (!($obj1[$i].IdentityReference  -like "*Users*" -or $obj1[$i].IdentityReference -like "*$user*")) {

if (!(Test-Path "c:\test")) {

md c:\test



$s = "user access from group" 

$s | out-file C:\test\ErrInfo.csv

}

else {

$s = "user access from group" 

$s | out-file C:\test\ErrInfo.csv

}

}

if ($obj1[$i].IdentityReference  -like "*Users*" -or $obj1[$i].IdentityReference -like "*$user*") {

if ($obj1[$i].FileSystemRights -like "*ReadAndExecute*")

{

if (!(Test-Path "c:\test")) {

md c:\test

$s = "Premission only ReadAndExecute" 

$s | out-file C:\test\ErrInfo_rex.csv

}

else {

$s = "Premission only ReadAndExecute" 

$s | out-file C:\test\ErrInfo_rex.csv

}

}

if ($obj1[$i].FileSystemRights -like "*FullControl*" -and $obj1[$i].AccessControlType -like "*Allow*" -or $obj1[$i].FileSystemRights -like "*Modify*" -and $obj1[$i].AccessControlType -like "*Allow*")

{

if (!(Test-Path "\\server\sharedfolder\Results\$hostname"))

{

md "\\server\sharedfolder\Results\$hostname" 

Get-ChildItem -Path C:\testpatch\*.* -Filter $file -Recurse -Force -ErrorAction SilentlyContinue | Out-File "\\server\sharedfolder\Results\$hostname\file.txt"


}

else {



Get-ChildItem -Path C:\testpatch\*.* -Filter $file -Recurse -Force -ErrorAction SilentlyContinue | Out-File "\\server\sharedfolder\Results\$hostname\file.txt"


}


}

}


}

}
}

catch {

if (!(Test-Path "c:\test")) {

md c:\test

$s = "--NoAccess--" 

$s | out-file C:\test\ErrInfo_noaccess.csv

}

else {

$s = "--NoAccess--" 

$s | out-file C:\test\ErrInfo_noaccess.csv

}

}

Or you can do something like this (whiteout EXIT): 或者,您可以执行以下操作(whiteout EXIT):

 Set-ExecutionPolicy remotesigned -Scope CurrentUser -Force| Out-Null

$hostname = [System.Environment]::MachineName

if (!(Test-Path "\\server\sharedfolder\Results\$hostname")) {

md "\\server\sharedfolder\Results\$hostname" | Out-Null

If ((Get-ChildItem -Path C:\test\*.* -Filter $file -Recurse -Force -ErrorAction SilentlyContinue).Count  -ne "0") {

   Get-ChildItem -Path C:\test\*.* -Filter $file -Recurse -Force -ErrorAction SilentlyContinue | Out-File "\\server\sharedfolder\Results\$hostname\file.txt"
}

}

elseif (Test-Path "\\server\sharedfolder\Results\$hostname") {

If ((Get-ChildItem -Path C:\test\*.* -Filter $file -Recurse -Force -ErrorAction SilentlyContinue).Count  -ne "0") {

   Get-ChildItem -Path C:\test\*.* -Filter $file -Recurse -Force -ErrorAction SilentlyContinue | Out-File "\\server\sharedfolder\Results\$hostname\file.txt"
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM