Powershell script to loop through variables multiple times - powershell

I have 4 variables and I want a script to execute these variables multiple times my thinking is placing each of the variables in their own file and then loop through the files and have the main script which takes the variables loop it.
I'm wondering if this is smart or if I can do it in a simpler way
basically it looks like this
File1
$SFTP_NAME = "PATH_1"
$CUSTOMER_NAME = "Customer_1"
$BACKUP_LOCATION = "Customer_1"
$IP_ADDRESS = 192.168.159.11
File2
$SFTP_NAME = "PATH_2"
$CUSTOMER_NAME = "Customer_2"
$BACKUP_LOCATION = "Customer_2"
$IP_ADDRESS = 192.168.159.12
Main Script
New-Item -ItemType directory -Path \\ftp\Customers\$SFTP_NAME\$CUSTOMER_NAME
New-Item -ItemType directory -Path \\ftp\Customers\$SFTP_NAME\$CUSTOMER_NAME\$BACKUP_LOCATION
New-Item -ItemType directory -Path \\ftp\Customers\$SFTP_NAME\$CUSTOMER_NAME\$IP_ADDRESS

I recommend one CSV file with four columns:
$configurations = #'
SFTP_NAME,CUSTOMER_NAME,BACKUP_LOCATION,IP_ADDRESS
PATH_1,Customer_1,Customer_1,192.168.159.11
PATH_2,Customer_2,Customer_2,192.168.159.12
'# | ConvertFrom-Csv
#or prepare csv file in Excel and import
#$configurations = Import-Csv Csvfile.csv
$configurations | % {
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.BACKUP_LOCATION)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.IP_ADDRESS)"
}

Create an array and store your configuration data in it using a custom object. Then loop trough the array and use the data to create your items.
$fileList = #()
$fileList += New-Object psobject -Property #{ "SFTP_NAME"="PATH_1"
"CUSTOMER_NAME"="Customer_1"
"BACKUP_LOCATION"="Customer_1"
"IP_ADDRESS"="192.168.159.11"}
$fileList += New-Object psobject -Property #{ "SFTP_NAME"="PATH_2"
"CUSTOMER_NAME"="Customer_2"
"BACKUP_LOCATION"="Customer_2"
"IP_ADDRESS"="192.168.159.12"}
$fileList | foreach {
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.BACKUP_LOCATION)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.IP_ADDRESS)"
}

Related

Powershell Script to create the file

I am new in Powershell. I tried to create the "dummy file" which is mentioned in the file.
For example, in the attached sheet, I have a file name called "Sheet" and there are total 5 files with different formats like - file1.pdf, file2.txt, file3.PNG, file4.docx, and file5.xlsx
I want to create the dummy file according to the file format.
So far I tried this code but it is not working. Please help me with it.
Thanks in advance
Screenshot:
So far I tried this code but not working -
$file_list = Get-Content -Path "C:\Users\JamesAlam\Downloads\Sheet.txt"
$destination_folder = "C:\Users\JamesAlam\Downloads\Example"
$null = New-Item -Path $destination_folder -ItemType Directory -Force
foreach ($file in $file_list) {
Copy-Item -Path $file -Destination $destination_folder -Recurse
}
You should usethe New-Item cmdlet to create the new files. Also, you can simplify your script. If you add -Force to the New-Item cmdlet, it will also create the directory structure:
$destination_folder = "C:\Users\JamesAlam\Downloads\Example"
Get-Content -Path "C:\Users\JamesAlam\Downloads\Sheet.txt" | ForEach-Object {
New-Item -Path (Join-Path $destination_folder $_) -Force
}

make multiple files in multiple folders

I look here on the forums but didn't find my answer.
I'm trying to make multiple folders on my C drive, and at the same time multiple files in these folders.
The folders are no problem, I'm using an array for that. But the files are not working :).
This is my script:
$folders = #("Test1","Test2","Test3")
$files = #("Test1.txt","Test2.txt","Test3.txt")
foreach ($folder in $folders) {
New-Item -ItemType Directory -Path c:\ -Name $folder
}
foreach ($file in $files){
New-Item -ItemType File -Path $folder -Name $file
}
Nest your loops:
$folders = #("Test1","Test2","Test3")
$files = #("Test1.txt","Test2.txt","Test3.txt")
foreach ($folder in $folders) {
# Create folder
$newFolder = New-Item -ItemType Directory -Path c:\ -Name $folder
foreach ($file in $files){
# Create the files in the folder
$null = New-Item -ItemType File -Path $newFolder.FullName -Name $file
}
}

Create Subfolders using Powershell

Ok, looking for some assistance with Powershell. I need to create a subfolder with the same name in about 200 folders in a directory. So far I have this:
$folder = NewFolderName
new-item -type directory -path \\servername\directory\directory\$folder -Force
Will this work to create the single folder in all 200 folders?
Try the following code snippet:
$parent = '\\servername\directory'
$folder = 'NewFolderName'
Get-ChildItem -Path $parent -Directory |
ForEach-Object {
New-Item -WhatIf -Type Directory -Path (
Join-Path -Path $_.FullName -ChildPath $folder) -Force
}
Remove the risk mitigation parameter -WhatIf no sooner than debugged…

PowerShell read csv file and create files accordingly

Basically, I have a CSV file with a lot of columns. Let's say it looks like this:
_username, platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
..., ...
I would like to write a script that goes through the file and for each platform, it creates a file with the specific username in a different folder, so I would have:
\platformX1\username1.file
\platformY\username2.file
\platformX2\username3.file, username4.file
etc etc...
I know I should use foreach with an if placed somewhere, but powershell is new for me, and I don't really know how to start it.
Here's something similar tweaked to what you want to do.
This created files of a specific size in your directory
$data = get-content "C:\Data\masteFile.csv"
$drcty = "C:\Data"
foreach($line in $data)
{
$a,$b = $line.Split("{,}")
$parent = $drcty+"\"+$a+"\"
$filename = $parent + $b.TRIM() +".txt"
write-host $filename
New-Item -ItemType directory -Path $parent
fsutil file createnew $filename 2000
}
this seems to do what you want. [grin]
# fake reading in a CSV file
# in real life, use Import-CSV
$InStuff = #'
_UserName, Platform_
username1, platformX1
username2, platformY
username3, platformX2
username4, platformX2
'# | ConvertFrom-Csv
$DestDir = $env:TEMP
$Extension = 'txt'
foreach ($IS_Item in $InStuff)
{
$TargetPath = Join-Path -Path $DestDir -ChildPath $IS_Item.Platform_
if (-not (Test-Path -LiteralPath $TargetPath))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $TargetPath -ItemType Directory
}
$FileName = $IS_Item._UserName, $Extension -join '.'
$FullFileName = Join-Path -Path $TargetPath -ChildPath $FileName
if (-not (Test-Path -LiteralPath $FullFileName))
{
# "$Null = " will supress unwanted output from New-Item
$Null = New-Item -Path $FullFileName -ItemType File
}
}
i think what it does is apparent, but i know i get ahead of where i otta be at times. so, if there are any questions, please feel free to ask ... [grin]
This worked for me.
$ErrorActionPreference = "Stop"
#Path of the CSV file resides
$csvData = Import-Csv -Path "$env:USERPROFILE\Desktop\data.csv"
$DestinationFolder = "C:\Stuffs"
foreach($record in $csvData)
{
$destPlatformFolder = $DestinationFolder + "\" + $record.platform_
if(-not(Test-Path -Path $destPlatformFolder)){
New-Item -Path $destPlatformFolder -ItemType Directory -Force | Out-Null
}
$destinationFile = $destPlatformFolder + "\" + $record._username
New-Item -Path $destinationFile -ItemType File -Force | Out-Null
}

How to create multiple subfolders?

How do I create a folder structure where I have one root folder, then approximately 9 differently named subfolders; within these subfolders there another set of differently named subfolders. I have already made one .csv file with the script. But I do not feel like writing out 30 lines of the same code to make these subfolders.
Here is what I have so far:
$LogsDir = "D:\Logs"
If (-Not (Test-Path $LogsDir)) {
New-Item -Path $LogsDir -ItemType Directory | Out-Null
} Else {
Write-Host "Directory already exists!"
}
$Subfolders1 = Import-Csv "Subfolders1.csv";
$Subfolders1 | Where-Object {$_.Type -eq 'Subfolder'} |
ForEach-Object {
$Subfolders1 = $_.Name;
New-Item "$LogsDir\$Subfolders1" -ItemType Directory
}
# ******sbf1 = the first set of subfolders******
# ******sf-** = the second set******
New-Item -Path "$LogsDir\sbf1\sf-a1" -ItemType Directory
New-Item -Path "$LogsDir\sbf1\sf-a2" -ItemType Directory
New-Item -Path "$LogsDir\sbf1\sf-a3" -ItemType Directory
New-Item -Path "$LogsDir\sbf2\sf-a1" -ItemType Directory
New-Item -Path "$LogsDir\sbf2\sf-a2" -ItemType Directory
New-Item -Path "$LogsDir\sbf2\sf-b2" -ItemType Directory
I am trying to create a log file folder tree. I think I got some of it down but any help would appreciated.
New-Item will create missing parent folders, so you only need a list of the leaf folders (with full path):
D:\Logs\sbf1\sf-a1
D:\Logs\sbf1\sf-a2
D:\Logs\sbf1\sf-a3
D:\Logs\sbf2\sf-a1
D:\Logs\sbf2\sf-a2
D:\Logs\sbf2\sf-b2
and then create the folders like this:
Get-Content 'C:\path\to\subfolders.txt' | % {
New-Item -Type Directory -Path $_
}