none
Create a WebVirtualDirectory in IIS for each user from a .csv file through PowerShell RRS feed

  • Question

  • Hello,

    I am trying to get a script to work to create virtual directories in IIS (6.1 or later) for each "user" in a .csv file. So far, this is what I have. i'm not sure what's wrong with it but any feedback would be appreciated:

    Import-Module WebAdministration

    $path = "d:\users_list.csv"

    $csv = Import-Csv -path $path

    for each($line in $csv)

    {

    $Name=$line.Name -as [int]

    {

    New-WebVirtualDirectory -Site "DefaultSite" -Name "Home\$Name" -PhysicalPath "d:\DefaultSite\$Name"

    }

    }

    Thank you.

    Wednesday, January 6, 2016 2:10 PM

Answers

  • Now carefully try this:

    $path=<enter your path>
    Import-Csv -path $path |
         %{
           if($_.Name.Trim()){
                Write-Host "Processing $($_.Name)" -fore green
                New-WebVirtualDirectory -Site 'Default Web Site' -Name "Home\$($_.Name)" -PhysicalPath "d:\DefaultSite\$($_.Name)" -ea 2 -WhatIf
           }else{
                Write-Host "NAME IS BLANK" -fore red
           }
         }


    \_(ツ)_/

    • Marked as answer by RoyalSoldier Thursday, January 7, 2016 3:01 PM
    Thursday, January 7, 2016 2:09 PM

All replies

  • What is this supposed to do?

    $Name=$line.Name -as [int]

    It doesn't produce a result.


    \_(ツ)_/

    Wednesday, January 6, 2016 2:31 PM
  • exactly, and that's my problem

    It's supposed to be the value that is in the field, e.g, the user, and use it as the folder name

    Wednesday, January 6, 2016 2:35 PM
  • So why are you trying to convert it to an  int?  Just use the field.

    \_(ツ)_/

    Wednesday, January 6, 2016 2:36 PM
  • I wasn't trying to; I just recycled a script and that's what it had.

    Anyway, good catch, but I still can't get it to do anything. The script runs and it shows me the command but it doesn't give me any other feedback and the virtual directories are not being created in IIS.

    I've also tried it with:

    New-Item d:\test\$Name -ItemType directory

    and nothing...

    Wednesday, January 6, 2016 2:53 PM
  • Looks like you need to learn how to use Windows and scripting.  Start here: http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/04/weekend-scripter-the-best-ways-to-learn-powershell.aspx


    \_(ツ)_/

    Wednesday, January 6, 2016 3:03 PM
  • It is your space in ForEach (for each)? Is it the value assigned to the -Site parameter? Should that be 'Default Web Site'?
    Wednesday, January 6, 2016 3:05 PM
  • $name will always be blank because you are trying to tur it into an int.  You don't want that line.  Just use the field.

    \_(ツ)_/

    Wednesday, January 6, 2016 3:06 PM
  • If I knew how to do it, I probably wouldn't be asking for help. Thanks for the link, but that doesn't address my issue.
    Wednesday, January 6, 2016 3:07 PM
  • It is your space in ForEach (for each)? Is it the value assigned to the -Site parameter? Should that be 'Default Web Site'?

    If that were true then there would be an error.  I think the user is trying to run this in notepad and, of course, it just sits there.

    In all cases the int line does not belong here.


    \_(ツ)_/

    Wednesday, January 6, 2016 3:09 PM
  • I took the line out and it still doesn't do anything.

    What I have is exactly the same script minus the "for each($line in $csv)" line.

    Wednesday, January 6, 2016 3:10 PM
  • Example:

    PS C:\scripts> for each($x in $y){}
    At line:1 char:4
    + for each($x in $y){}
    +    ~
    Missing opening '(' after keyword 'for'.
    At line:1 char:13
    + for each($x in $y){}


    \_(ツ)_/

    Wednesday, January 6, 2016 3:10 PM
  • If I knew how to do it, I probably wouldn't be asking for help. Thanks for the link, but that doesn't address my issue.

    Without some basic understanding there is no way to help you see why what you are doing won't work.

    This forum is for questions from technicians and Admins who are trained in Windows and computer technologies.  It is not a help desk for end users.  It is required that you spend the time needed to learn the basics of the technology you are trying to use.

    This is also not a free consulting forum.

    Three tumes now you have been given a solution to try and you have just added a new complaint without event trying the suggested solution.  This is nearly always because of a lack of fundamental knowledge of the technology.  That knowledge is your responsibility.  The link posted will help you acquire the minimum required understanding so you can carry on an intelligent discussion.


    \_(ツ)_/

    Wednesday, January 6, 2016 3:16 PM
  • I took the line out and it still doesn't do anything.

    What I have is exactly the same script minus the "for each($line in $csv)" line.

    Now you are just playing.  Even a new user can't be that clueless.  Why would you remove that line? 


    \_(ツ)_/

    Wednesday, January 6, 2016 3:17 PM
  • This is actually a good example of why copy and paste programming is mostly ineffective at solving a real-world problem.

    In other words, you can't just copy and paste code and expect it to work magically without understanding what the code is doing. As has already been noted, you need to start by learning the basics of the language (in this case, PowerShell) that you are trying to use.

    I understand that this kind of response can be frustrating when you're just trying to get some work done, but the principle is that these concepts are reusable and learning them will definitely pay off in the long run.

    In the example jrv posted, there is an immediate error. I will give you a hint about the problem: for is not equivalent to foreach. They are two different constructs, and each behaves differently.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 6, 2016 3:22 PM
    Moderator
  • This is actually a good example of why copy and paste programming is mostly ineffective at solving a real-world problem.

    In other words, you can't just copy and paste code and expect it to work magically without understanding what the code is doing. As has already been noted, you need to start by learning the basics of the language (in this case, PowerShell) that you are trying to use.

    I understand that this kind of response can be frustrating when you're just trying to get some work done, but the principle is that these concepts are reusable and learning them will definitely pay off in the long run.

    In the example jrv posted, there is an immediate error. I will give you a hint about the problem: for is not equivalent to foreach. They are two different constructs, and each behaves differently.


    -- Bill Stewart [Bill_Stewart]

    Bill, if I'm in the wrong place, as a moderator, that's all you have to tell me. The only reason I'm here is because I'm looking for help to find out what is wrong with the script that I have. I am not here to debate with other users about my expertise or to get told "go learn how to do it". I have better things to do, and I imagine everyone else here does as well.
    Thank you for your hint, however.

    Wednesday, January 6, 2016 3:34 PM
  • ...or to get told "go learn how to do it". I have better things to do...

    What we're trying to say is that to learn the technology, rather than being a waste of your time (which you are implying by saying you have "better things to do"), is actually a very productive use of your time.

    For example, if you learn how the foreach loop works, and how it is different from for, then you don't need to wonder why your posted example doesn't work.

    As I am sure you already know, Microsoft has positioned PowerShell as the primary means of automating Windows and Microsoft enterprise applications. Third-party vendors are also supplying PowerShell libraries to manage their products. You’re going to have to learn PowerShell anyway, so you might as well start now.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 6, 2016 3:42 PM
    Moderator
  • I though the following was pretty explicit:

    QUOTE FROM ABOVE:  >>>>

    If I knew how to do it, I probably wouldn't be asking for help. Thanks for the link, but that doesn't address my issue.

    Without some basic understanding there is no way to help you see why what you are doing won't work.

    >>>>>>

    The issue was addressed but you couldn't understand.  We cannot come to your site to type it for you.  I recommended removing the "-As [int]".  YOu didn't seem to know how to even edit a line.

    IN the future think about how the person or group you are asking will perceive the question.  Do you know enough about the technology t even ask a question.  Also qask yyurself if you have read the help, searched the net for the issue or error.

    Here is an example of what you will get with a simple search: https://www.google.com/?gws_rd=ssl#newwindow=1&q=examples+of+New-WebVirtualDirectory


    \_(ツ)_/

    Wednesday, January 6, 2016 3:49 PM
  • No, I'm sorry, I wasn't suggesting in any way that learning how to script in PS is a waste of time. If you will, note that I was specifically talking about debating and such.
    Wednesday, January 6, 2016 4:26 PM
  • $name will always be blank because you are trying to tur it into an int.  You don't want that line.  Just use the field.

    \_(ツ)_/

    I took out the line because that's what you said. Before that, and I think you just missed it, I said I also tried to remove only the -as [int], and that didn't do anything. I also tried the same with a different command: New-Item d:\test\$Name -ItemType directory and it also didn't work. However, according to you and the moderator, I need to learn PS scripting before coming here looking for help. So, forget it; there is nothing else to say, except:

    1) I am not trying to run the script in notepad (this is just ignorant to suggest, along with the rest of the unnecessary remarks you've been making throughout the entire thread)
    2) all you know is that I need help working on a PS script; you cannot infer anything else from that.
    3) you've been mocking me the entire time and I don't even know why the moderator, who was here, hasn't done anything about it.

    This has been a very unhelpful and disappointing experience.

    Wednesday, January 6, 2016 4:38 PM
  • Sorry you're disappointed, but a peer-to-peer web forum is an extremely inefficient way to learn how to program in any language (scripting is a form of programming, after all).

    We can infer inexperience in PowerShell due to the basic nature of the question (you wrote "for each" rather than "foreach", which won't work). Inexperience isn't bad - we all start somewhere - but it is not realistic to expect respondents to train you in basic details. There are several reasons for this:

    1. It is inefficient

    2. Respondents are volunteers--there is no SLA

    3. There are already lots of free resources available to help you learn PowerShell basics


    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 6, 2016 4:56 PM
    Moderator

  • This has been a very unhelpful and disappointing experience.

    You have been asking for someone to blindly debug a script that you copied from the Internet.  It is highly likely that the script never worked and that we are being asked to do the  impossible along with you being unable to provide any assistance due to your ignorance of scripting or the technology.

    If you have read the forum rules and FAQ - which any experienced user would do first - you would see that you cannot ask for others t do your work for you and that you are expected to understand the basics.

    As such your repeated vague responses - I know you cannot see that they were vague - were more and more annoying. 

    Do not take this personally.  Spend some time catching up on the technology and come back with a specific question.  You will find that once you know a little bit more that the users here can be very helpful.  We just cannot compensate for complete lack of experience.

    Good luck and please take the time to learn PowerShell.  It only takes a few days to get a good start.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 6, 2016 5:07 PM
    Wednesday, January 6, 2016 5:05 PM

  • This has been a very unhelpful and disappointing experience.

    You have been asking for someone to blindly debug a script that you copied from the Internet.  It is highly likely that the script never worked and that we are being asked to do the  impossible along with you being unable to provide any assistance due to your ignorance of scripting or the technology.

    If you have read the forum rules and FAQ - which any experienced user would do first - you would see that you cannot ask for others t do your work for you and that you are expected to understand the basics.

    As such your repeated vague responses - I know you cannot see that they were vague - were more and more annoying. 

    Do not take this personally.  Spend some time catching up on the technology and come back with a specific question.  You will find that once you know a little bit more that the users here can be very helpful.  We just cannot compensate for complete lack of experience.

    Good luck and please take the time to learn PowerShell.  It only takes a few days to get a good start.


    \_(ツ)_/


    If you look at my original post, it says what I was looking for: feedback. "Go learn scripting" is not proper feedback.  My understanding of PS is sufficient for what I've had to deal with up to now. I encountered an issue that I wasn't ready to solve by myself and I went looking for help. Now you want to tell me that I shouldn't have come here, that I was violating the forum rules and, all along, that I'm some random end-user that can't even use Windows properly.
    You've been disrespectful, and generally unhelpful. The entire thread, in the relevant sense, was a waste of everyone's time. There is nothing here that would help anyone looking for help with the topic.
    I've already recommended the thread be cleared.


    Wednesday, January 6, 2016 6:13 PM
  • What is this supposed to do?

    $Name=$line.Name -as [int]

    It doesn't produce a result.


    \_(ツ)_/

    I asked a question and didn't get an answer.  You didn't tell me what it was supposed to do.  Every answer uyou gave was completely irrelevant to the question asked.  I only recommended that you use the learning material after you made it clear that you had absolutely no clue as to what was being answered or how to use the basic information given.

    In short you could not be communicated with in any way.  What would you have expected anyone to do.  We cannot make you understand when you do not even have an understanding of the basic language.

    Don't feel bad.  There are now a huge number of people who have a computer and thing they know something.  None have any training in computer engineering and insist they can understand.  I guess anyone can become a brain surgeon by just asking questions on the Internet.  We are doomed. 


    \_(ツ)_/

    Wednesday, January 6, 2016 6:22 PM
  • Sorry you're disappointed, but a peer-to-peer web forum is an extremely inefficient way to learn how to program in any language (scripting is a form of programming, after all).

    We can infer inexperience in PowerShell due to the basic nature of the question (you wrote "for each" rather than "foreach", which won't work). Inexperience isn't bad - we all start somewhere - but it is not realistic to expect respondents to train you in basic details. There are several reasons for this:

    1. It is inefficient

    2. Respondents are volunteers--there is no SLA

    3. There are already lots of free resources available to help you learn PowerShell basics


    -- Bill Stewart [Bill_Stewart]

    Again, I wasn't trying to learn how to program, and I wasn't expecting anyone to teach me anything; I simply asked for feedback about my script. Anyone not being able, or willing, to answer the request should have simply left it alone. In fact, moderator, you should have deleted the thread if it was inappropriate. Instead, along with the other user, you tried turning this into some teaching lesson about the value of programming and whatnot.
    No thanks.

    Wednesday, January 6, 2016 6:24 PM
  • exactly, and that's my problem

    It's supposed to be the value that is in the field, e.g, the user, and use it as the folder name

    Actually, I did say what it was supposed to do.
    Wednesday, January 6, 2016 6:27 PM
  • So why are you trying to convert it to an  int?  Just use the field.

    \_(ツ)_/

    And I answered you pointing out that you are making it an [int] which will not work. Yu cannot turn text that is not numeric into an int.


    \_(ツ)_/

    Wednesday, January 6, 2016 6:31 PM
  • and I know I already pointed this out, but I've also said that I removed the "-as [int]", and it's still not working. It doesn't give me any errors at all.
    Wednesday, January 6, 2016 6:49 PM
  • There is absolutely no way you can run that code and get no output.  You are not telling us something very basic.

    Open PowerShell CLI py typing this at a prompt:

    powershell -noprofile

    Now copy and paste this into the prompt.

    Import-Module WebAdministration
    New-Item -ItemType Directory -path c:\testsite
    New-WebVirtualDirectory -Site 'Default Web Site' -Name testsite -PhysicalPath c:\testsite

    You will either get  site or an error no other items are possible.


    \_(ツ)_/




    • Edited by jrv Wednesday, January 6, 2016 7:03 PM
    Wednesday, January 6, 2016 7:02 PM
  • The script gives me no errors but it shows that it runs. It shows the New-WebVirtualDirectory line as many times as there are users in the .csv file but it doesn't actually create the virtual directory in IIS.
    I can create the virtual directories through PS in IIS, manually, by specifying the folder names, for instance what you wrote.
    So, the output looks exactly the same, except when I run it in the script, with the variables, it states the path with the variables, without creating the folders.
    Wednesday, January 6, 2016 7:34 PM
  • Did you do exactly what I asked you to do?  I don't care about your old file we already know that there is an issue.  You have not posted your whole script and you have not posted your profile.  Somewhere you have disabled error reporting and have not told us.  This makes all of this just a big guessing game.


    \_(ツ)_/

    Wednesday, January 6, 2016 7:44 PM
  • I ran exactly what you told me to run. It works without issues. Also, no, I haven't disabled error reporting. I get errors, typically when there is an error in the command I am trying to run.

    Wednesday, January 6, 2016 8:32 PM
  • So how can we help? We have provided feedback to you.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, January 6, 2016 8:36 PM
    Moderator
  • $path=<enter your path>
    Import-Csv -path $path | %{ Write-Host "Processing $($_.Name)" -fore green New-WebVirtualDirectory -Site "DefaultSite" -Name "Home\$($_.Name)" -PhysicalPath "d:\DefaultSite\$($_.Name)" -ea 2 }

    Now start a new session and run this.


    \_(ツ)_/

    Wednesday, January 6, 2016 8:42 PM
  • While with my script it was processing the information from the csv, here it does not. It does tell me that it created a single virtual folder called home, but that's it.  It essentially ignores "$($_.Name)", just as it was in my script and it doesn't report any errors at all. (it did say "Processing" in green though!)
    Wednesday, January 6, 2016 9:06 PM
  • Just do this:

    $path=<enter your path>
    Import-Csv -path $path |
         %{
           if($_.Name.Trim()){
                Write-Host "Processing $($_.Name)" -fore green
           }else{
                Write-Host "NAME IS BLANK" -fore red
           }
         }

    Do NOT use ISE.  Use CLI for this.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 6, 2016 10:19 PM
    Wednesday, January 6, 2016 9:20 PM
  • it errors telling me that it's missing ')' after expression in 'if' statement

    Wednesday, January 6, 2016 9:49 PM
  • Hi,

    why two {{ ?? foreach together..

    Import-Module WebAdministration

    $path = "d:\users_list.csv"

    $csv = Import-Csv -path $path

    foreach($line in $csv)

    {

    $Name=$line.Name -as [int]

    New-WebVirtualDirectory -Site "DefaultSite" -Name "Home\$Name" -PhysicalPath "d:\DefaultSite\$Name"

    }


    Hello Guys, i have a problem. How can i restore reminder messages in Outlook or Exchange?. Background: If i get a reminder message in Outlook and Close this with "Close All" i lost all reminders. So my Question can i restore my reminders? Best regards Aleksej Kusnir

    Wednesday, January 6, 2016 9:51 PM
  • it errors telling me that it's missing ')' after expression in 'if' statement

    Correct. Look up the syntax for the if statement and see if you can tell what's wrong:


    PS C:\> help about_If

    (This actually reinforces our earlier point: If you learn the basics, a simple typo in a posted command won't stop you in your tracks. You can simply add the missing character.)


    -- Bill Stewart [Bill_Stewart]


    Wednesday, January 6, 2016 9:57 PM
    Moderator
  • Stuff happens.  Copy it again.  Its fixed.


    \_(ツ)_/

    Wednesday, January 6, 2016 10:20 PM
  • It tells me "processing "user name"" for each user listed in my .csv.
    Thursday, January 7, 2016 1:57 PM
  • Now carefully try this:

    $path=<enter your path>
    Import-Csv -path $path |
         %{
           if($_.Name.Trim()){
                Write-Host "Processing $($_.Name)" -fore green
                New-WebVirtualDirectory -Site 'Default Web Site' -Name "Home\$($_.Name)" -PhysicalPath "d:\DefaultSite\$($_.Name)" -ea 2 -WhatIf
           }else{
                Write-Host "NAME IS BLANK" -fore red
           }
         }


    \_(ツ)_/

    • Marked as answer by RoyalSoldier Thursday, January 7, 2016 3:01 PM
    Thursday, January 7, 2016 2:09 PM
  • That's perfect, jrv. It works flawlessly and I got it to also create system folders and apps and their system folders! Thank you for your help. 
    Thursday, January 7, 2016 3:01 PM