Advertisement
Combreal

uaUser.ps1

Feb 27th, 2020 (edited)
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. This script is just a wrapper of fm's ua-user command
  4.  
  5. .DESCRIPTION
  6. This script creates a super account for the specified user on the specified machine.
  7. It can also create a homedir if needed.
  8.  
  9. .PARAMETER machineName
  10. Used to specify the machine name.
  11.  
  12. .PARAMETER login
  13. Used to specify the user login.
  14.  
  15. .PARAMETER homedir
  16. Used to specify if a homedir should be created.
  17. y or n. No is set by default.
  18.  
  19.  
  20. .EXAMPLE
  21. .\uaUser.ps1 -machineName "mymachine" -login "conio"
  22.  
  23. .EXAMPLE
  24. .\uaUser.ps1 -machineName "mymachine" -login "conio" -HomeDir "y"
  25. #>
  26.  
  27. param
  28. (
  29.     [string]$machineName = "NotSet",
  30.     [string]$login = "NotSet",
  31.     [string]$HomeDir = "n"
  32. )
  33.  
  34. $command = "/usr/local/bin/ua-user -sa "
  35.  
  36. if($machineName -eq "NotSet") {
  37.     Write-Host "The Machine's name was not entered."
  38.     Exit
  39. }
  40. else {
  41.     $machineName = $machineName + ".dbs.fr"
  42. }
  43.  
  44. if($login -eq "NotSet") {
  45.     Write-Host "User's login was not entered."
  46.     Exit
  47. }
  48.  
  49.  
  50. $cred = Get-StoredCredential -Target sshConn
  51. $ssho  = New-SSHSession -ComputerName $machineName -Credential $cred
  52. $streamyo = New-SSHShellStream -Index $ssho.SessionId
  53.  
  54. $streamyo.WriteLine('sudo -i')
  55. Start-Sleep -Milliseconds 400
  56. if($HomeDir -eq "n") {
  57.     $command = $command + $login
  58.     $streamyo.WriteLine($command)
  59. }
  60. else {
  61.     $command = "echo y | " + $command + "-a " + $login
  62.     $streamyo.WriteLine($command)
  63. }
  64. Start-Sleep -Milliseconds 800
  65. $result = $streamyo.read()
  66. Write-Host $result
  67.  
  68. Remove-SSHSession $ssho 2>&1>$null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement