如何针对每个用户配置文件运行这些命令?

时间:2019-06-14 19:46:54

标签: powershell

我旨在创建一个脚本来在系统概要文件上创建各种文件。我试图让脚本搜索然后在每个配置文件上运行,但是我很挣扎。以下是我到目前为止的摘要。我是脚本编写的新手。我以为ForEach循环将是此应用程序的最佳选择,但我不确定出错的地方。

$profilelist = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
ForEach ($profile in $profilelist)
{
# Create directories

$directory = 1
do
{
    New-Item -Path C:\Users\$profile\Desktop\$directory -Type Directory
    $directory
    $directory++

}while($directory-lt 6)

# Create files

Get-ChildItem -Path C:\Users\$profile\Desktop
New-Item -Path * -Name passwords.txt -Type File
New-Item -Path * -Name PII.txt -Type File

# Write Data

Get-ChildItem -Path C:\Users\$profile\Desktop
Add-Content -Path C:\Users\$profile\Desktop\*\passwords.txt -Value "Citibank.com: password123", "Amazon.com: qwerty", "Wellsfargo.com: deadshotwashere", "crypsislabs@group.com: 1337passw0rd"
Add-Content -Path C:\Users\$profile\Desktop\*\PII.txt -Value "SSN: 153-02-9283", "License ID: 41 928 2942", "Bank Account: 938429517"

# Create ZIP file from files located on Desktop
Get-ChildItem -Path C:\Users\$profile\Desktop
Compress-Archive -Path * -CompressionLevel Optimal -DestinationPath C:\Users\$profile\Downloads\test.zip

1 个答案:

答案 0 :(得分:2)

$profilelist是一个字符串,因此您可能不想对其进行迭代。要获取所有ProfileImagePath属性,可以使用以下命令:

$allProfiles = @((dir $profilelist | Get-ItemProperty).profileImagePath | Where-Object { $_ -notlike 'C:\Windows*' })

这将输出:

C:\Users\user1
C:\Users\user2

,依此类推。 @( ... )是为了确保即使只有一个匹配的配置文件,您也可以得到一个数组。

请注意,我排除了默认的Windows配置文件,因为您可能仍然不希望使用它们。请记住将$profilelist中的$allProfiles更改为ForEach

相关问题