Create a non-interactive PowerShell script that accepts the following parameters on the command line:

Hello,
Please to help me.
How to create a script for this:
Create a non-interactive PowerShell script that accepts the following parameters on the command line:

a. ProjectStart
b. ProjectEnd
c. PhaseStart
d. PhaseEnd
So, if the user supplied 1, 3, 1, and 2 as corresponding parameter values, then the following folder
structure shall be created:
C:\Projects\Project1\Phase1
C:\Projects\Project1\Phase2
C:\Projects\Project2\Phase1
C:\Projects\Project2\Phase2
C:\Projects\Project3\Phase1
C:\Projects\Project3\Phase2

Welcome to PowerShell.org forums.

Can you let us know the background if this script, for what this script which you will be writing will be used ?

The background is as follows. I have to write it for homework (Homework - Scripts and Command Line)

Main goal is to create two PowerShell scripts. This time aren’t any specific requirements about the infrastructure.
Tasks
Do the following:
1. Create an interactive PowerShell script that asks the user for:
a. First name
b. Last name
c. Password
And based on the user’s input creates:
- AD user with SamAccountName with the following format First_name.Last_name
- Personal (named) folder in C:\Shared on the computer where the script is executed
For example if user executed the script on the DC and entered for first name Name, for last name LastName,
and for password Password1, then a new AD user with SamAccountName equal to FirstName.LastName will be
created. In addition, a new folder C:\Shared\FirstName.LastName will be created as well.
Then the script should ask the user if he/she wants to create another user or no.
2. Create a non-interactive PowerShell script that accepts the following parameters on the command line:
a. ProjectStart
b. ProjectEnd
c. PhaseStart
d. PhaseEnd
So, if the user supplied 1, 3, 1, and 2 as corresponding parameter values, then the following folder
structure shall be created:
C:\Projects\Project1\Phase1
C:\Projects\Project1\Phase2
C:\Projects\Project2\Phase1
C:\Projects\Project2\Phase2
C:\Projects\Project3\Phase1
C:\Projects\Project3\Phase2

Proof
Send/attach the scripts

I am ready with the first condition

anybody can you help me?

Great to know that you have done the part 1. Let us know where you are stuck in writing code for second condition.

I don’t know how to bake it. They gave him this code but it doesn’t work.

foreach ($project in $projectStart..$projectEnd) {
    $phaseStart..$phaseEnd | Foreach-Object {
        $DirectoryParams = @{
            Path = "C:\Projects\Project{0}\Phase{1}" -f $project,$_
            ItemType = 'Directory'
            ErrorAction = 'SilentlyContinue'
        }
        New-Item @DirectoryParams -WhatIf
    }
}

@nightclubtraffic Is this part of an learning project or assignments ?

task from a system administrator course

Ahh. Then you should definitely try writing some code yourself based on training you have attended. Getting a direct solution from any forums will defeat the purpose of the course .

Try putting up some code, it should be easy for this task for sure. Folks here will definitely help if you have some code :grinning:.

1 Like
Param (
[int]$ProjectStart,
[int]$ProjectEnd,
[int]$PhaseStart,
[int]$PhaseEnd
)
New-Item -Path C: -Name Projects -ItemType directory

[int]$value = Read-Host -Prompt "Write Value"

for ($i=$ProjectStart;$i -le $ProjectEnd;$i++){
New-Item -Path C:\Projects -Name Project$value -ItemType directory -Force
    for ($j = $PhaseStart;$j -le $ProjectEnd;$j++){
    new-item -Path C:\Projects\Project$value -Name Phase$value -ItemType directory -Force
    }
}

Instead of $value for your outer loop you should use $i. And for your inner loop you should use $j. The rest looks good to me. :+1:

Param (
[int]$ProjectStart,
[int]$ProjectEnd,
[int]$PhaseStart,
[int]$PhaseEnd
)
New-Item -Path C: -Name Projects -ItemType directory

[int]$i = Read-Host -Prompt "Write Value"

for ($i=$ProjectStart;$i -le $ProjectEnd;$i++){
New-Item -Path C:\Projects -Name Project$value -ItemType directory -Force
    for ($j = $PhaseStart;$j -le $ProjectEnd;$j++){
    new-item -Path C:\Projects\Project$value -Name Phase$value -ItemType directory -Force -WhatIf
    }
}

> Blockquote
PS C:\Users\borislav.stoyanov> Param (
[int]$ProjectStart,
[int]$ProjectEnd,
[int]$PhaseStart,
[int]$PhaseEnd
)
New-Item -Path C: -Name Projects -ItemType directory

[int]$i = Read-Host -Prompt "Write Value"

for ($i=$ProjectStart;$i -le $ProjectEnd;$i++){
New-Item -Path C:\Projects -Name Project$value -ItemType directory -Force
    for ($j = $PhaseStart;$j -le $ProjectEnd;$j++){
    new-item -Path C:\Projects\Project$value -Name Phase$value -ItemType directory -Force -WhatIf
    }
}


    Directory: C:\Users\PC


Mode                 LastWriteTime         Length Name                                                                               
----                 -------------         ------ ----                                                                               
d-----       6.4.2021 г.      9:04                Projects                                                                           
Write Value: 3

    Directory: C:\Projects


Mode                 LastWriteTime         Length Name                                                                               
----                 -------------         ------ ----                                                                               
d-----       6.4.2021 г.      9:00                Project1                                                                           
What if: Performing the operation "Create Directory" on target "Destination: C:\Projects\Project1\Phase1".



if I knew how to do it, I wouldn’t ask you for help.
Really ask you for the script, and then see your mistakes.
thank you

So, where did you get this code then? It does not help you if you don’t understand the code.

You have an outer loop with its variable $i and you have an inner loop with its variable $j. And you should use them to create the desired directories. … that’s not that hard to understand. :wink:

Param (
[int]$ProjectStart,
[int]$ProjectEnd,
[int]$PhaseStart,
[int]$PhaseEnd
)
New-Item -Path C: -Name Projects -ItemType directory

for ($i = $ProjectStart; $i -le $ProjectEnd; $i++){
New-Item -Path C:\Projects -Name Project$i -ItemType directory -Force
    for ($j = $PhaseStart; $j -le $ProjectEnd; $j++){
        New-Item -Path C:\Projects\Project$i -Name Phase$j -ItemType directory -Force
    }
}

We work this code with my colleague and from there.
We’re trying to drive him. Now I will try if it will happen.
thanks