将文本文件中的条目读入PowerShell中的变量

时间:2022-11-19 13:31:50

I hope someone can help me. I am pretty new to PowerShell and can't really script it myself with the exception of looking at existing code and modifying it.

我希望有一个人可以帮助我。我是PowerShell的新手,除了查看现有代码并修改它之外,我自己无法编写脚本。

I have found a PowerShell script that reports file share permissions for a specific share and recurses through the subfolders returning their permissions as well.

我找到了一个PowerShell脚本,它报告特定共享的文件共享权限,并通过返回其权限的子文件夹进行递归。

My problem is I need to do this with a lot of shares so would like to be able to provide the script with a text file containing the share names. I know I need to do a for each loop and read the names of the shares in a text file into an array but I don't know how to do this. I guess it's pretty simple for someone with more experience.

我的问题是我需要使用大量共享来执行此操作,因此希望能够为脚本提供包含共享名称的文本文件。我知道我需要为每个循环执行一个操作,并将文本文件中的共享名称读入数组,但我不知道如何执行此操作。我想对于有更多经验的人来说这很简单。

This is the script i have used with single entry.

这是我用单输入的脚本。

http://mywinsysadm.wordpress.com/2011/08/17/powershell-reporting-ntfs-permissions-of-windows-file-shares/

http://mywinsysadm.wordpress.com/2011/08/17/powershell-reporting-ntfs-permissions-of-windows-file-shares/

#Set variables
$path = Read-Host "Enter the path you wish to check"
$filename = Read-Host "Enter Output File Name"
$date = Get-Date

#Place Headers on out-put file
$list = "Permissions for directories in: $Path"
$list | format-table | Out-File "C:\scripts\$filename"
$datelist = "Report Run Time: $date"
$datelist | format-table | Out-File -append "C:\scripts\$filename"
$spacelist = " "
$spacelist | format-table | Out-File -append "C:\scripts\$filename"

#Populate Folders & Files Array
[Array] $files = Get-ChildItem -path $path -force -recurse

#Process data in array
ForEach ($file in [Array] $files)
{
#Convert Powershell Provider Folder Path to standard folder path
$PSPath = (Convert-Path $file.pspath)
$list = ("Path: $PSPath")
$list | format-table | Out-File -append "C:\scripts\$filename"

Get-Acl -path $PSPath | Format-List -property AccessToString | Out-File -append "C:\scripts\$filename"

} #end ForEach

Sorry for the noob question. I plan to learn more when I have a bit more time but any help now would be massively appreciated.

抱歉,这个菜鸟问题。我计划在我有更多时间的时候学到更多东西,但现在任何帮助都会受到大力赞赏。

Thanks in advance.

提前致谢。

1 个解决方案

#1


7  

If you have a share name on each line within your text file can put all the shares into an array like this:

如果文本文件中的每一行都有一个共享名称,则可以将所有共享放入一个数组中,如下所示:

$path = "C:\ShareNames.txt"
$shareArray = gc $path

To access the first share you can use this syntax:

要访问第一个共享,您可以使用以下语法:

$shareArray[0] 

#1


7  

If you have a share name on each line within your text file can put all the shares into an array like this:

如果文本文件中的每一行都有一个共享名称,则可以将所有共享放入一个数组中,如下所示:

$path = "C:\ShareNames.txt"
$shareArray = gc $path

To access the first share you can use this syntax:

要访问第一个共享,您可以使用以下语法:

$shareArray[0]