some one can help with powershell ? I am new in that


  1. Posts : 4
    win 10 x64
       #1

    some one can help with powershell ? I am new in that


    i will post the part of the script have probleme with
      My Computer


  2. Posts : 3,274
    Win10
       #2

    I am not very good at powershell myself, but there are others on the forum who are, so if you provide a snippet of the code you are having a problem with maybe someone who knows may guide you.
      My Computers


  3. Posts : 4
    win 10 x64
    Thread Starter
       #3

    here is the code
    copy and run it you will see the error


    Code:
        Add-Type -AssemblyName System.Windows.Forms
        Add-Type -AssemblyName System.Drawing
    
        Function AddEm
        {
            $TextBox4.Text = $TextBox1.Text + " " + $TextBox2.Text
            $Textbox3.Visible = $true
            $Textbox3.Text = $Textbox1.Text[0] + "" + $Textbox2.Text + "@email.com"
            $Textbox3.Visible = $true
        }
    
    Function MakeForm
    {
        $script:Form1 = New-Object system.Windows.Forms.Form
        $Form1.Text = "Add Mail box"
        $Font = New-Object System.Drawing.Font("Times New Roman", 12, [System.Drawing.FontStyle]::Bold)
        $Form1.Font = $Font
        
        $label = New-Object Windows.Forms.Label
        $label.Location = New-Object Drawing.Point 150, 80
        $label.Size = New-Object Drawing.Point 200, 25
        $label.text = "First Name"
        $Form1.Controls.Add($label)
        $script:TextBox1 = New-Object System.Windows.Forms.Textbox
        $TextBox1.Location = New-Object System.Drawing.Size(5, 80)
        $TextBox1.Size = New-Object System.Drawing.Size(100, 40)
        
        $label = New-Object Windows.Forms.Label
        $label.Location = New-Object Drawing.Point 150, 113
        $label.Size = New-Object Drawing.Point 200, 25
        $label.text = "Name"
        $Form1.Controls.Add($label)    
        $script:TextBox2 = New-Object System.Windows.Forms.Textbox
        $TextBox2.Location = New-Object System.Drawing.Size(5, 110)
        $TextBox2.Size = New-Object System.Drawing.Size(100, 40)
        
        $label = New-Object Windows.Forms.Label
        $label.Location = New-Object Drawing.Point 150, 143
        $label.Size = New-Object Drawing.Point 200, 25
        $label.text = "Full Name"
        $Form1.Controls.Add($label)    
        $script:TextBox4 = New-Object System.Windows.Forms.Textbox
        $TextBox4.Location = New-Object System.Drawing.Size(5, 140)
        $TextBox4.Size = New-Object System.Drawing.Size(120, 40)
        
        $script:TextBox3 = New-Object System.Windows.Forms.Textbox
        $TextBox3.Location = New-Object System.Drawing.Size(5, 210)
        $TextBox3.Size = New-Object System.Drawing.Size(200, 40)
        $TextBox3.Visible = $false
            
        $Button1 = New-Object System.Windows.Forms.Button
        $Button1.Location = New-Object System.Drawing.Size(95, 20)
        $Button1.Size = New-Object System.Drawing.Size(80, 50)
        $Button1.Text = "Create"
        $Button1.Add_Click({                 
                    Set-ExecutionPolicy Unrestricted -Scope Process -Force
                    $Username = "admin@.onmicrosoft.com"
                    $Password = ConvertTo-SecureString 'Password' -AsPlainText -Force
                    $LiveCred = New-Object System.Management.Automation.PSCredential $Username, $Password
                    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection
                    Import-PSSession $Session
                    Connect-MsolService -Credential $LiveCred
                    Import-Module MSOnline
                    Get-MsolUser
                    New-MsolUser -UserPrincipalName "$x" -FirstName "$textbox1" -LastName "$textbox2" -DisplayName "$textbox4" -UsageLocation CA
                    Set-MsolUserPassword -UserPrincipalname "$x" -NewPassword "$password1"
                    
                    $forcechange = Read-Host "force a changer Y/N"
                    if ($forcechange -eq "n")
                    {
                        Set-MsolUserPassword -UserPrincipalName "$x" -NewPassWord "$nouveaupass" -ForceChangePassword $false
                    }
                    else
                    {
                        Set-MsolUserPassword -UserPrincipalName "$x" -NewPassWord "$nouveaupass" -ForceChangePassword $true
                    }
                    Get-MsolAccountSku
                    $LicenseSKU = Get-MsolAccountSku | Out-GridView -Title 'Select a license plan to assign to users' -OutputMode Single | Select-Object -ExpandProperty AccountSkuId
                    $x = Get-MsolUser -All -UnlicensedUsersOnly | Out-GridView -Title 'Select users to assign license plan to' -OutputMode Multiple
                    $x | Set-MsolUser -UsageLocation CA
                    $x | Set-MsolUserLicense -AddLicenses $LicenseSKU
                })
                        $Button2 = New-Object System.Windows.Forms.Button
            $Button2.Location = New-Object System.Drawing.Size(5, 20)
            $Button2.Size = New-Object System.Drawing.Size(80, 50)
            $Button2.Text = "Creation email"
            $Button2.Add_Click({ AddEm })
            
            $Button3 = New-Object System.Windows.Forms.Button
            $Button3.Location = New-Object System.Drawing.Size(185, 20)
            $Button3.Size = New-Object System.Drawing.Size(80, 50)
            $Button3.Text = "Exit"
            $Button3.Add_Click({ $Form1.Close() })
            
            $Form1.Controls.Add($Button1)
            $Form1.Controls.Add($Button2)
            $Form1.Controls.Add($Button3)
            $Form1.Controls.Add($TextBox1)
            $Form1.Controls.Add($TextBox2)
            $Form1.Controls.Add($TextBox3)
            $Form1.Controls.Add($TextBox4)
            $Form1.ShowDialog()
            }
            
                $Button6 = New-Object System.Windows.Forms.Button
        $Button6.Location = New-Object System.Drawing.Size(25, 15)
        $Button6.Size = New-Object System.Drawing.Size(110, 23)
        $Button6.Text = "Créé Usager"
        $Button6.Add_Click({ MakeForm -form1 })
        
        
            
        Add-Type -AssemblyName System.Windows.Forms
        Add-Type -AssemblyName System.Drawing
        
        # Build Form
        $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "O365 Manager"
        $Form.Size = New-Object System.Drawing.Size(800, 800)
        $Form.StartPosition = "CenterScreen"
        $Form.Topmost = $false
        
            $form.Controls.Add($Button6)
        $form.Controls.Add($Button7)
        
        #Add Button event 
        $Button6.Add_Click($Button6)
        $Button7.Add_Click($Button7)
        
        $form.ShowDialog()
    Last edited by Brink; 20 May 2019 at 11:55. Reason: code box
      My Computer


  4. Posts : 4
    win 10 x64
    Thread Starter
       #4

    sorry but that is only a part of the code

    those error when it's whit the code it's work

    error are in variable $textbox3 2 1 ans 4
      My Computer


  5. Posts : 1,223
    W10-Pro 22H2
       #5

    MArtyboy05 said:
    sorry but that is only a part of the code
    ... and you expect us to help? Notwithstanding the fact that it calls for a connection to Office365 (which prooduces more errors for me), adds a click function to Button7 which hasn't been created, and function Makeform is never completed, my system never makes it far enough to flag up an error with those textboxes.

    Post a code snippet that functions (but with errors) without external extras, and we may be able to help...
      My Computer


  6. Posts : 9,785
    Mac OS Catalina
       #6

    MArtyboy05 said:
    sorry but that is only a part of the code

    those error when it's whit the code it's work

    error are in variable $textbox3 2 1 ans 4
    Your code is incomplete and will produce errors dor those who are trying to replicate your error in a Sandbox or VM. See this about writing PS scripts for Office365.

    PowerShell Scripts for Office 365

    https://docs.microsoft.com/en-us/off...365-powershell
    Ten Most Useful Office 365 PowerShell Commands
    Deep Dive Into Office 365 PowerShell Cmdlets (Part 1)
    Remote Office >> ADMIN Magazine
    Huge List Of PowerShell Commands for Active Directory, Office 365 and more

    The biggest issue is that you have not defined any parameters. A script is a process that runs how humans perform an ordered task, using a Flowchart methodology.
      My Computer


  7. Posts : 9,785
    Mac OS Catalina
       #7

    Also see https://devblogs.microsoft.com/scrip...rshell-script/ for tracing the execution.

    This is your main error:

    "Get-MsolUser : The term 'Get-MsolUser' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

    The error is because there is no Exchange directory for the script to connect to to verify users. Without that as the case is with Azure needing an AD and Exchange, the script will not get any further when running it. You can see it by setting Trace with the cmdlet Set-PSDebug -trace .
      My Computer


  8. Posts : 4
    win 10 x64
    Thread Starter
       #8

    I know all of the error can you juste run the script and where i put first lastname email and full name those you fill it up in the second form the textbox i write to be viriable and goes back to the script

    $Textbox1 2 3 4 when the script run it put for the user creation $textbox3 the userprincipalname some one know it doesn't use the variable ?
      My Computer


  9. Posts : 9,785
    Mac OS Catalina
       #9

    MArtyboy05 said:
    I know all of the error can you juste run the script and where i put first lastname email and full name those you fill it up in the second form the textbox i write to be viriable and goes back to the script

    $Textbox1 2 3 4 when the script run it put for the user creation $textbox3 the userprincipalname some one know it doesn't use the variable ?
    You can only use -msoluser not -msoluserpassword. There is no cmdlet -msoluserpassword.

    Use the trace command in PS, to find where your errors are in the complete script.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 01:53.
Find Us




Windows 10 Forums