none
Powershell. Cant receive data from Rixhbox RRS feed

  • Question

  • Hello. I cant receive data from Rixhbox from the interface. If you do not use the GUI, everything works fine, what do I do wrong? The script below

    function New-Share {
    
        param(
    
            [string] $ComputerName = $env:COMPUTERNAME,
    
            [string] $Path = 'C:\Temp',
    
            [string] $ShareName = 'Temp',
    
            [string] $AccountName = 'Domain Users',
    
            [ValidateSet('FullControl', 'Change','Read')] $AccessPermissions = 'Read',
    
            [string] $ShareDescription
    
        )
    
     
    
        # Convert the AccessPermissions
    
        $accessFlags = @{
    
            FullControl = 2032127
    
            Change = 1245631
    
            Read = 1179817
    
        }; $access = $accessFlags[$AccessPermissions]
    
     
    
        # Extract Domain and User account
    
        $Domain, $Identity = if($AccountName -match '\\') {
    
            $AccountName -split '\\'
    
        } else {
    
            $env:USERDOMAIN, $AccountName
    
        }
    
     
    
        # Build the Security Descriptor and Trustee objects
    
        $sd = ([wmiclass] "\\$ComputerName\root\cimv2:Win32_SecurityDescriptor").CreateInstance()
    
        $trustee = ([wmiclass] "\\$ComputerName\root\cimv2:Win32_Trustee").CreateInstance()
    
        $trustee.Name = $Identity
    
        $trustee.Domain = $Domain
    
     
    
            # Build the Access Control Entry object
    
        $ace = ([wmiclass] "\\$ComputerName\root\cimv2:Win32_ACE").CreateInstance()
    
        $ace.AccessMask = $access
    
        $ace.AceFlags = 3 
    
        $ace.AceType = 0 # 0 Allow, 1 = Deny
    
        $ace.Trustee = $trustee 
    
        $sd.DACL = $ace.psObject.BaseObject
    
     
    
        # Create the share with the required permissions
    
        $mc = [wmiclass]"\\$ComputerName\root\cimv2:Win32_Share"
    
        $inParams = $mc.psbase.GetMethodParameters('Create')
    
        $inParams.Access = $sd
    
        $inParams.Description = $ShareDescription
    
        $inParams.MaximumAllowed = $null
    
        $inParams.Name = $ShareName
    
        $inParams.Password = $null
    
        $inParams.Path = $Path
    
        $inParams.Type = [uint32]0
    
        $ret = $mc.psbase.InvokeMethod('Create',$inParams, $null)
    
     
    
        # Determine the return value from the WMI method
    
        Switch ($ret.ReturnValue){
    
            0  { Write-Verbose 'Share created successfully'; break }
    
            2  { Write-Error 'Access denied (2)'; break }
    
            8  { Write-Error 'Unknown failure (8)'; break }
    
            9  { Write-Error 'Invalid name (9)'; break }
    
            10 { Write-Error 'Invalid level (10)'; break }
    
            21 { Write-Error 'Invalid parameter (21)'; break }
    
            22 { Write-Error 'Duplicate share (22)'; break }
    
            23 { Write-Error 'Redirected path (23)'; break }
    
            24 { Write-Error 'Unknown device or directory (24)'; break }
    
            25 { Write-Error 'Net name not found (25)'; break }
    
            default { Write-Error 'Other Error (26–4294967295)' }
    
        }
    
    }
    
    function Add-ShareAccess {
    
        param(
    
            [string] $ComputerName = $env:COMPUTERNAME,
    
            [string] $ShareName = 'Temp',
    
            [string] $AccountName = 'Domain Users',
    
            [ValidateSet('FullControl', 'Change','Read')] $AccessPermissions = 'Read'
    
        )
    
     
    
        # Convert the AccessPermissions
    
        $accessFlags = @{
    
            FullControl = 2032127
    
            Change = 1245631
    
            Read = 1179817
    
        }; $access = $accessFlags[$AccessPermissions]
    
     
    
        # Extract Domain and User account
    
        $Domain, $Identity = if($AccountName -match '\\') {
    
            $AccountName -split '\\'
    
        } else {
    
            $env:USERDOMAIN, $AccountName
    
        }
    
     
    
        # Build the Security Descriptor and Trustee objects
    
        $sd = ([wmiclass] "\\$ComputerName\root\cimv2:Win32_SecurityDescriptor").CreateInstance()
    
        $trustee = ([wmiclass] "\\$ComputerName\root\cimv2:Win32_Trustee").CreateInstance()
    
        $trustee.Name = $Identity
    
        $trustee.Domain = $Domain
    
     
    
        # Build the Access Control Entry object
    
        $ace = ([wmiclass] "\\$ComputerName\root\cimv2:Win32_ACE").CreateInstance()
    
        $ace.AccessMask = $access
    
        $ace.AceFlags = 3 
    
        $ace.AceType = 0 # 0 Allow, 1 = Deny
    
        $ace.Trustee = $trustee 
    
     
    
        # Get the current permissions on the share
    
        $current = Get-WmiObject -ComputerName $computerName -Class Win32_LogicalShareSecuritySetting -Filter "Name='$ShareName'"
    
        [System.Management.ManagementBaseObject[]]$newACE = $current.GetSecurityDescriptor().Descriptor.DACL
    
     
    
        # Add the access control entry to the list
    
        [array]::Resize([ref]$newACE, $newACE.Count + 1)
    
        $newACE[$newACE.Count-1] = $ace
    
        $sd.DACL = $newACE
    
     
    
        # Set the permissions on the share
    
        $mc = Get-WmiObject -ComputerName $computerName -Class Win32_Share -Filter "Name='$ShareName'"
    
        $ret = $mc.SetShareInfo($null, $mc.Description, $sd)
    
     
    
        # Determine the return value from the WMI method
    
        Switch ($ret.ReturnValue){
    
            0  { Write-Verbose 'Share updated successfully'; break }
    
            2  { Write-Error 'Access denied (2)'; break }
    
            8  { Write-Error 'Unknown failure (8)'; break }
    
            9  { Write-Error 'Invalid name (9)'; break }
    
            10 { Write-Error 'Invalid level (10)'; break }
    
            21 { Write-Error 'Invalid parameter (21)'; break }
    
            22 { Write-Error 'Duplicate share (22)'; break }
    
            23 { Write-Error 'Redirected path (23)'; break }
    
            24 { Write-Error 'Unknown device or directory (24)'; break }
    
            25 { Write-Error 'Net name not found (25)'; break }
    
            default { Write-Error 'Other Error (26–4294967295)' }
    
        }
    
    }
    
     
    
    #------------------------- end share func
    
     
    
    Function Testpath {
    
    param($SharePath) #param
    
    if(!(Test-Path $SharePath))
    
    {New-Item -Path $SharePath -ItemType Directory 
    
    Write-Host $SharePath "ok" -ForegroundColor Green
    
    }
    
    else
    
    {
    
    Write-Host $SharePath "exist" -ForegroundColor Cyan
    
    }
    
    }
    
     
    
    #---------------------------- end test path
    
    #$servers = $null
    
    #$path = $null
    
    #$sharename = $null
    
    $user = "domain\user"
    
    $permission = "Change"
    
     
    
     
    
    function go {
    
    param ($srv, $path, $sharename)
    
    Foreach ($srv in $servers){
    
    New-Share -ComputerName $srv -Path $path -ShareName $sharename  -AccountName $user  -AccessPermissions $permission -ShareDescription GG
    
    }
    
    }
    
     
    
     
    
    [void][System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms") 
    
    [void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic") 
    
    [void][System.Windows.Forms.Application]::EnableVisualStyles()
    
    $form = New-Object "System.Windows.Forms.Form"; 
    
    $form.Width = 800; 
    
    $form.Height = 600;   
    
    $form.Text = "Addv0.1"; 
    
    $form.StartPosition = "CenterScreen" 
    
    $form.MaximizeBox = $false        
    
    $form.FormBorderStyle = 'Fixed3d' 
    
     
    
    #----------------path
    
    $richbox5 = New-Object System.Windows.Forms.RichTextBox
    
    $richbox5.Location = New-Object System.Drawing.Size(240,170)
    
    $richbox5.Size = New-Object System.Drawing.Size(350,23)
    
    $richbox5.ScrollBars = "Vertical"
    
    $richbox5.Multiline = $true
    
    $form.Controls.Add($richbox5)
    
     
    
    #---------------srv
    
    $richbox1 = New-Object System.Windows.Forms.RichTextBox
    
    $richbox1.Location = New-Object System.Drawing.Size(240,130)
    
    $richbox1.Size = New-Object System.Drawing.Size(350,23)
    
    $richbox1.ScrollBars = "Vertical"
    
    $richbox1.Multiline = $true
    
    $richbox1.Text = "serv"
    
    $form.Controls.Add($richbox1)
    
     
    
    #---------------sharename
    
    $richbox2 = New-Object System.Windows.Forms.RichTextBox
    
    $richbox2.Location = New-Object System.Drawing.Size(240,90)
    
    $richbox2.Size = New-Object System.Drawing.Size(350,23)
    
    $richbox2.ScrollBars = "Vertical"
    
    $richbox2.Multiline = $true
    
    $richbox2.Text = "sharename"
    
    $form.Controls.Add($richbox2)
    
     
    
    #----------------check
    
    $CheckboxAddColl = New-Object System.Windows.Forms.CheckBox
    
    $CheckboxAddColl.Location = New-Object System.Drawing.Point (570,40)
    
    $CheckboxAddColl.AutoSize = $true
    
    $CheckboxAddColl.Checked = $false
    
    $CheckboxAddColl.Text =  "RL"
    
    $form.Controls.Add($CheckboxAddColl) 
    
    #------------------btn
    
    $Run = New-Object System.Windows.Forms.Button
    
    $Run.Location = New-object System.Drawing.Size(670,260)
    
    $Run.Size = New-Object System.Drawing.Size(100,40)
    
    $Run.Text = "run"
    
    $Run.Add_Click({if ($CheckboxAddColl.Checked){
    
    Testpath $Script:richbox5.Text
    
    Start-Sleep -Seconds 5
    
    go $Script:richbox1.Text
    
    go $Script:richbox5.text
    
    go $Script:richbox2.Text
    
    }
    
     
    
     
    
     
    
    #if ($CheckboxAddTime.Checked){AccountExp}
    
    #if ($CheckboxAddAdmin.Checked){AddAdmin}
    
    })
    
    $form.Controls.Add($Run)
    
    $form.ShowDialog() 


    • Edited by Sel83 Friday, December 15, 2017 10:35 AM
    • Moved by Bill_Stewart Friday, January 26, 2018 3:36 PM This is not "fix/debug/rewrite my script for me" forum
    Friday, December 15, 2017 10:34 AM

All replies

  • There is no way that we can debug all of you code and try yo understand what you are asking.

    To get the contents of a "RichTextBox" control just reference the "Text" property.

    $myvar = $richtextbox1.Text


    \_(ツ)_/

    Friday, December 15, 2017 10:42 AM
  • A quick look at the mess of code shows that it does nothing.  There is no code that is functional.  It loads some functions which are never used and it displays a form that does nothing.

    You cannot just blindly paste together a bunch of code copied from various places then ask someone here to fix it. 

    Start by learning PowerShell then make you code work without forms.  Next learn forms and how to use them with PowerShell.  After that you will be able to see how to call functions from a form and how to design a script that does this correctly.


    \_(ツ)_/

    Friday, December 15, 2017 10:57 AM
  • Here is a quick example of how to use a form to supply data to a function.

    #requires -Version 3
    function Show-DlgGetInfo {
        
        Add-Type -AssemblyName System.Windows.Forms
        
        #----------------------------------------------
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $form1 = New-Object 'System.Windows.Forms.Form'
        $buttonCreate = New-Object 'System.Windows.Forms.Button'
        $groupbox1 = New-Object 'System.Windows.Forms.GroupBox'
        $radiobuttonFull = New-Object 'System.Windows.Forms.RadioButton'
        $radiobuttonModify = New-Object 'System.Windows.Forms.RadioButton'
        $radiobuttonRead = New-Object 'System.Windows.Forms.RadioButton'
        $textboxShare = New-Object 'System.Windows.Forms.TextBox'
        $labelShare = New-Object 'System.Windows.Forms.Label'
        $textboxServerName = New-Object 'System.Windows.Forms.TextBox'
        $Server = New-Object 'System.Windows.Forms.Label'
        $buttonOK = New-Object 'System.Windows.Forms.Button'
        $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
        
        function New-Share {
            Param (
                $Server,
                $ShareName,
                $Everyone
            )
            # code to crate teh share
            Write-Host $Server $ShareName $Everyone
        }
        $form1_Load = {
            
        }
        
        $buttonCreate_Click = {
            $perm = ($groupbox1.Controls | Where-Object{ $_.Checked }).Tag
            New-Share $textboxServerName.Text $textboxShare.Text $perm
        }
        
        $Form_StateCorrection_Load =
        {
            #Correct the initial state of the form to prevent the .Net maximized form issue
            $form1.WindowState = $InitialFormWindowState
        }
        
        $form1.SuspendLayout()
        $groupbox1.SuspendLayout()
        #
        # form1
        #
        $form1.Controls.Add($buttonCreate)
        $form1.Controls.Add($groupbox1)
        $form1.Controls.Add($textboxShare)
        $form1.Controls.Add($labelShare)
        $form1.Controls.Add($textboxServerName)
        $form1.Controls.Add($Server)
        $form1.Controls.Add($buttonOK)
        $form1.AcceptButton = $buttonOK
        $form1.AutoScaleDimensions = '11, 20'
        $form1.AutoScaleMode = 'Font'
        $form1.ClientSize = '704, 285'
        $form1.Font = 'Microsoft Sans Serif, 10.2pt, style=Bold'
        $form1.FormBorderStyle = 'FixedDialog'
        $form1.Margin = '7, 6, 7, 6'
        $form1.MaximizeBox = $False
        $form1.MinimizeBox = $False
        $form1.Name = 'form1'
        $form1.StartPosition = 'CenterScreen'
        $form1.Text = 'Form'
        $form1.add_Load($form1_Load)
        #
        # buttonCreate
        #
        $buttonCreate.Location = '544, 35'
        $buttonCreate.Margin = '6, 5, 6, 5'
        $buttonCreate.Name = 'buttonCreate'
        $buttonCreate.Size = '138, 35'
        $buttonCreate.TabIndex = 8
        $buttonCreate.Text = 'Create'
        $buttonCreate.UseCompatibleTextRendering = $True
        $buttonCreate.UseVisualStyleBackColor = $True
        $buttonCreate.add_Click($buttonCreate_Click)
        #
        # groupbox1
        #
        $groupbox1.Controls.Add($radiobuttonFull)
        $groupbox1.Controls.Add($radiobuttonModify)
        $groupbox1.Controls.Add($radiobuttonRead)
        $groupbox1.BackColor = 'GradientInactiveCaption'
        $groupbox1.Location = '47, 97'
        $groupbox1.Margin = '6, 5, 6, 5'
        $groupbox1.Name = 'groupbox1'
        $groupbox1.Padding = '6, 5, 6, 5'
        $groupbox1.Size = '367, 154'
        $groupbox1.TabIndex = 7
        $groupbox1.TabStop = $False
        $groupbox1.Text = 'Everyone:'
        $groupbox1.UseCompatibleTextRendering = $True
        #
        # radiobuttonFull
        #
        $radiobuttonFull.Location = '67, 107'
        $radiobuttonFull.Margin = '6, 5, 6, 5'
        $radiobuttonFull.Name = 'radiobuttonFull'
        $radiobuttonFull.Size = '191, 37'
        $radiobuttonFull.TabIndex = 2
        $radiobuttonFull.TabStop = $True
        $radiobuttonFull.Tag = 'Full'
        $radiobuttonFull.Text = 'Full'
        $radiobuttonFull.UseCompatibleTextRendering = $True
        $radiobuttonFull.UseVisualStyleBackColor = $True
        #
        # radiobuttonModify
        #
        $radiobuttonModify.Location = '67, 66'
        $radiobuttonModify.Margin = '6, 5, 6, 5'
        $radiobuttonModify.Name = 'radiobuttonModify'
        $radiobuttonModify.Size = '191, 37'
        $radiobuttonModify.TabIndex = 1
        $radiobuttonModify.TabStop = $True
        $radiobuttonModify.Tag = 'Modify'
        $radiobuttonModify.Text = 'Modify'
        $radiobuttonModify.UseCompatibleTextRendering = $True
        $radiobuttonModify.UseVisualStyleBackColor = $True
        #
        # radiobuttonRead
        #
        $radiobuttonRead.Location = '67, 30'
        $radiobuttonRead.Margin = '6, 5, 6, 5'
        $radiobuttonRead.Name = 'radiobuttonRead'
        $radiobuttonRead.Size = '191, 37'
        $radiobuttonRead.TabIndex = 0
        $radiobuttonRead.TabStop = $True
        $radiobuttonRead.Tag = 'Read'
        $radiobuttonRead.Text = 'Read'
        $radiobuttonRead.UseCompatibleTextRendering = $True
        $radiobuttonRead.UseVisualStyleBackColor = $True
        #
        # textboxShare
        #
        $textboxShare.Location = '147, 60'
        $textboxShare.Margin = '6, 5, 6, 5'
        $textboxShare.Name = 'textboxShare'
        $textboxShare.Size = '180, 27'
        $textboxShare.TabIndex = 4
        #
        # labelShare
        #
        $labelShare.AutoSize = $True
        $labelShare.Location = '47, 63'
        $labelShare.Margin = '6, 0, 6, 0'
        $labelShare.Name = 'labelShare'
        $labelShare.Size = '60, 25'
        $labelShare.TabIndex = 3
        $labelShare.Text = 'Share:'
        $labelShare.UseCompatibleTextRendering = $True
        #
        # textboxServerName
        #
        $textboxServerName.Location = '147, 32'
        $textboxServerName.Margin = '6, 5, 6, 5'
        $textboxServerName.Name = 'textboxServerName'
        $textboxServerName.Size = '180, 27'
        $textboxServerName.TabIndex = 2
        #
        # Server
        #
        $Server.AutoSize = $True
        $Server.Location = '47, 35'
        $Server.Margin = '6, 0, 6, 0'
        $Server.Name = 'Server'
        $Server.Size = '65, 25'
        $Server.TabIndex = 1
        $Server.Text = 'Server:'
        $Server.UseCompatibleTextRendering = $True
        #
        # buttonOK
        #
        $buttonOK.Anchor = 'Bottom, Right'
        $buttonOK.DialogResult = 'OK'
        $buttonOK.Location = '544, 231'
        $buttonOK.Margin = '6, 5, 6, 5'
        $buttonOK.Name = 'buttonOK'
        $buttonOK.Size = '138, 35'
        $buttonOK.TabIndex = 0
        $buttonOK.Text = '&OK'
        $buttonOK.UseCompatibleTextRendering = $True
        $buttonOK.UseVisualStyleBackColor = $True
        $groupbox1.ResumeLayout()
        $form1.ResumeLayout()
        
        #Save the initial state of the form
        $InitialFormWindowState = $form1.WindowState
        #Init the OnLoad event to correct the initial state of the form
        $form1.add_Load($Form_StateCorrection_Load)
    
        return $form1.ShowDialog()
        
    } 
    
    Show-DlgGetInfo
    


    \_(ツ)_/

    Friday, December 15, 2017 11:26 AM
  • There is no way that we can debug all of you code and try yo understand what you are asking.

    To get the contents of a "RichTextBox" control just reference the "Text" property.

    $myvar = $richtextbox1.Text


    \_(ツ)_/

    it work only after restarting the script. i need help with this part:

    $Run = New-Object System.Windows.Forms.Button
    
    $Run.Location = New-object System.Drawing.Size(670,260)
    
    $Run.Size = New-Object System.Drawing.Size(100,40)
    
    $Run.Text = "run"
    
    $Run.Add_Click({if ($CheckboxAddColl.Checked){
    
    Testpath $Script:richbox5.Text
    
    Start-Sleep -Seconds 5
    
    go $Script:richbox1.Text
    
    go $Script:richbox5.text
    
    go $Script:richbox2.Text
    
    }
    thanks for the attached example, I'll try it


    • Edited by Sel83 Saturday, December 16, 2017 8:12 AM
    Saturday, December 16, 2017 8:05 AM
  • My example code demonstrates how to do what you need to do.  That is the best I can do.  If you do not know PowerShell or Forms then you will have to either learn or contact a consultant to help you.


    \_(ツ)_/

    Saturday, December 16, 2017 8:08 AM