Powershell double loop Username Password
Hi,
I have a question:
I need to create DTAP environment thats equal. ( the users are local users special for a FTP site). It are about 1200 usernames.we are using W2012R2 with powershell 4.0.
I have 2 txt files, username.txt and password.txt
I am having problems with a double loop.
The main problem that my second get content add all the information in one line so i have a password that has all the information from the full txt file.
Do i have to use: Foreach-object? or is there another solution to create a username and password. that use the content correctly?
for example :
username.txt
Ronald
David
Dennis
password.txt
@@#$@#$F
@#$@#$F@#$F
the lines having a one on one relationship so : username Ronald has as password FRW@##$$%
Iam doing the following
Import-Module WebAdministration
Function CreateFTPUsers
{
$users = Get-Content c:\work\username.txt
$password = get-content c:\work\password.txt
Foreach ($_ in $users)
{
Foreach ($_P in $password)
{
#Creates Local users
NET USER "$_" "$password" /ADD
NET LOCALGROUP "FTPUsers" "$_" /ADD
Write-Host "Created user $_ with password $password"
$LogDir = "c:\inetpub\logs\ftpsvc\"
$FTPUserDir = "c:\work\ftp\"
$FTPUserPath = ($Username.Split("\")[1])
New-Item IIS:\Sites\FTP\$FTPUserDir\$FTPUserPath -Type VirtualDirectory -PhysicalPath "$LogDir" -force
Add-WebConfiguration -Filter /System.FtpServer/Security/Authorization -Value (@{AccessType="Allow"; Users="$FTPUserPath"; Permissions="Read, Write"}) -PSPath IIS: -Location "FTP/$FTPUserDir/$FTPUserPath"
Clear-WebConfiguration -Filter /System.FtpServer/Security/Authorization -PSPath IIS: -Location "FTP/$FTPUserDir"
}
}
CreateFTPUsers
}
}
Answers (2)
Then you can loop through the content and use Split to separate them into the required strings.
Comments:
-
To elaborate further on VBSCab.
$UserNamesAndPasswords = (Import-Csv $DataCSV -Header UserName,Password)[1..($DataCSV.length - 1)]
The [1..($DataCSV.length - 1)] part removes the header line in the CSV, omit this line if you do not have a header.
You can use for ForEach loop to cycle through the contents of the array.
ie
ForEach ($_ in $UserNamesAndPasswords)
{Do something here
Write-Host $_.UserName
Write-Host $_.Password} - rileyz 9 years ago
$users = Get-Content "username.txt"
$passwords = Get-Content "password.txt"
# Quit if files are not the same length
if ($users.count -ne $passwords.count) {Exit}
$count = $users.count - 1
0..$count | ForEach-Object {
$user = [string] $users[$_]
$password = [string] $passwords[$_]
Write-Output "User: $user Password: $password"
}