0
Function Get-RandoPass{

    $csv = "C:\Users\Administrator\Desktop\words.csv"
    $WordList = Import-Csv $csv

    $list1 = $WordList.List1
    $list2 = $WordList.List2
    $list3 = $Wordlist.List3
    
    $word1 = Get-Random -InputObject $list1 -Count 1
    $word2 = Get-Random -InputObject $list1 -Count 1
    $word3 = Get-Random -InputObject $list1 -Count 1
    $num1 = Get-Random -Maximum 10 -Minimum 0
    Write-Host "Random Password:" $word1$num1-$word2-$word3
}
Get-RandoPass| Export-Csv -Notypeinformation -Path C:\Users\Administrator\Desktop\test2.csv

So I'm trying to modify this script so it exports the password generated to a csv but it only exports a blank file. Not sure what I'm doing wrong on that one. The other thing I'm trying to figure out how to do is set it up so I can generate [x] number of passwords which I think I'd just have to add a switch parameter on a loop to rerun the password generation but I could be wrong. I'm pretty new to powershell.

Any ideas would be appreciated!

EDIT: Here's an example from the csv, it does generate the passwords as expected but it just doesn't export them to a csv file.

Dictionary csv

Some Dude
  • 5
  • 2
  • can you add portion of the `words.csv` file – Deepan Aug 13 '22 at 12:33
  • @Deepan I've added an example in an edit. It does generate the passwords, just doesn't export em' to a csv. – Some Dude Aug 13 '22 at 12:42
  • One more doubt can you add the required format (sample data) for the `test2.csv` file – Deepan Aug 13 '22 at 12:44
  • Posted a new edit. – Some Dude Aug 13 '22 at 12:51
  • [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`). – mklement0 Aug 13 '22 at 13:34

2 Answers2

2

Your function Get-RandomPassword does not output anything..
It just writes randomized passwords to the console window.
That is why you pipe nothing to the Export-Csv cmdlet. What the function should do is to emit the passwords as objects

Replace the line Write-Host "Random Password:" $word1$num1-$word2$num2-$word3$num3 with:

[PsCustomObject]@{Password = "$word1$num1-$word2$num2-$word3$num3"}

That way the function outputs an array of random password objects you can pipe through to the Export-Csv cmdlet

Theo
  • 57,719
  • 8
  • 24
  • 41
  • can you share any books/websites to learn powershell, I'm a beginner – Deepan Aug 13 '22 at 13:07
  • 1
    Well... there are lots of books like `PowerShell Cookbook` or `Learn Windows PowerShell in a Month of Lunches`, but also a LOT of online tutorials to be found, like [tutorialspoint](https://www.tutorialspoint.com/powershell/index.htm) and [Learning PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/00-introduction). Just google for it, you will find the stuff you need ;). BTW if you feel my answer solved your problem, please take a few moments to read the StackOverFlow [tour] to see how you can mark the question 'done' (and others may benefit too) – Theo Aug 13 '22 at 13:18
0

Try this code this will generate the password, I have added a for loop to generate 10 different password.

Function Get-RandomPassword {
  Param(
   [Parameter(Mandatory=$false,Position=1)]
   [string]$csv
   ) #end param

    $csv = "D:\new\new1.csv"
    $WordList = Import-Csv $csv

    $list1 = $WordList.List1
    $list2 = $WordList.List2
    $list3 = $Wordlist.$list3

    for ($i = 1; $i -le 10; $i++) {
    $word1 = Get-Random -InputObject $list1 -Count 1
    $word2 = Get-Random -InputObject $list2 -Count 1
    $word3 = Get-Random -InputObject $list3 -Count 1
    $num1 = Get-Random -Maximum 10 -Minimum 0
    $num2 = Get-Random -Maximum 30 -Minimum 20
    $num3 = Get-Random -Maximum 99 -Minimum 31
    [PsCustomObject]@{Password = $(Write-Output $word1$num1-$word2$num2-$word3$num3)}
    }
}
Get-RandomPassword | Export-Csv -UseQuotes AsNeeded  -Path "D:\new\test1.csv" 

Output:

Angery4-Bird25-business47
Bored0-Cat25-call44
Amused2-Cow25-by97
Blissful3-Elephant27-camera95
Amused9-Chicken24-but97
Acceped6-Chicken25-by74
Acceped3-Cow20-call72
Amused7-Elephant20-ability32
Anxious5-Cat26-buy96
Bored6-Ant28-ability70

Thanks to @Theo learned new thing PsCustomObject.

Deepan
  • 430
  • 4
  • 13
  • Get-Random : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:15 char:40 + $word1 = Get-Random -InputObject $list1 -Count 1 + ~~~~~~ + CategoryInfo : InvalidData: (:) [Get-Random], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetRandomCommand Returns this error when I try it. I'll take a poke to see what i'm doing wrong! – Some Dude Aug 13 '22 at 13:00
  • Have you changed $csv variable? – Deepan Aug 13 '22 at 13:02
  • 1
    Deleted the library file and then rebuilt it and then it worked. Not sure why... – Some Dude Aug 13 '22 at 13:11