Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Singol Thread Error (STA) when open script by powershell

$
0
0

hi everyone. this is my script:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

function global:stamp(){
param([string]$string)
$string=$string.replace(' ',',')
$string | Out-File "C:\test.txt" -Append
$form.close()
}

$form = New-Object System.Windows.Forms.Form

[int32]$x=(Get-WmiObject -Class Win32_DesktopMonitor | ForEach-Object {$_.ScreenWidth}) - 100
[int32]$y=(Get-WmiObject -Class Win32_DesktopMonitor | ForEach-Object {$_.ScreenHeight}) - 100
$x1 = $x - 100
$y1 = $y - 400

$form.Size = New-Object System.Drawing.Size($x,$y)

$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size($x1,$y1)

$dataGridView.Location = New-Object System.Drawing.Size(100,40)

$dataGridView.AutoSizeColumnsMode = "Fill"

$go = New-Object System.Windows.Forms.Button
$go.Location = New-Object System.Drawing.Size(300,450)
$go.Size = New-Object System.Drawing.Size(80,30)
$go.text = "ok"
$form.Controls.Add($go)
$form.Controls.Add($dataGridView)
$Column0 = New-Object System.Windows.Forms.DataGridViewTextboxColumn
$Column0.HeaderText = "Pacchetti"
$Column0.Width = 300
$dataGridView.Columns.Add($Column0)
$Column1 = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$Column1.HeaderText = "Azione"
$Column1.width = 300
$Options="Promozione","Regressione"
$Column1.Items.AddRange($Options)
$dataGridView.Columns.Add($Column1)
$Column2 = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
$Options2="Promozione","Regressione", "Null"
$Column2.HeaderText = "Tipo_Dipendenza"
$Column2.Width = 300
$Column2.Items.AddRange($Options2)
$dataGridView.Columns.Add($Column2)
$Column3 = New-Object System.Windows.Forms.DataGridViewTextboxColumn
$Column3.HeaderText = "Pacchetti_Dipendenti"
$Column3.Width = 300
$dataGridView.Columns.Add($Column3)

$go.Add_Click(
{
    $p=$true
    $intestazione="Pkt,Azione,Dip,PktDip" | Out-File "C:\test.txt"
    $data=New-Object System.Collections.ArrayList
    for($j=0;$j -le ($dataGridView.RowCount - 2); $j++){
    #$dataGridView.Rows[$j].Selected = $true
    for($i=0;$i -le ($dataGridView.Columns.Count -1);$i++){
    $export=$dataGridView.Rows[$j].Cells[$i].Value
    $columnName = $dataGridView.Columns[$i].HeaderText
    $riga = $j+1
    switch ($i){
    0 { if(!($log=cmd /c hudp -b harvest -en HarvestOper -st Accettazione -usr c.nosari -pw harvest -pn QueryPkg -ap $export -o " C:\log.txt" | Select-String -Pattern "NON ESISTE")){$data.Add($export)}
        else{[System.Windows.Forms.MessageBox]::Show("Campo $columnName riga $riga  Errato","WARNING",0,48)
    $p=$false
    }}
    1 {$data.Add($export)}
    2 {$data.Add($export)}
    3 { if(!($log=cmd /c hudp -b harvest -en HarvestOper -st Accettazione -usr c.nosari -pw harvest -pn QueryPkg -ap $export -o " C:\log.txt" | Select-String -Pattern "NON ESISTE") -or $export -eq ""){$data.Add($export)}
        else{[System.Windows.Forms.MessageBox]::Show("Campo $columnName riga $riga  Errato","WARNING",0,48)
    $p=$false
    }}
    }
    if(!($p)){break}  
    }
    if($p){
    stamp $data 
    $data.clear()
    }
    else{
    break
    }
    }
})

[void]$form.ShowDialog() 

When i execute it by powershell i receive thi error:

System.ThreadingThreadStateException:Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
System.Windows.Forms.ComboBoxset_AutoCompleteSource(Autoco
mpleteSourcevalue)
System.Windows.Forms.DataGridViewComboBoxCell.InitializeEditingCo
ntrol(1nt32 rowlndex,, ObjectinitialFormattedValue,,
DataGridViewCellStyledataGridViewCellStyle)
System.Windows.Forms.DataGridViewJnitializeEditingControlValue(Dat
aGridViewCellStyle&dataGridViewCellStyle,,DataGridViewCell
DataGridViewCell)
To replacethisdefault dialog box,handle the event
DataError.

How can i fix this? where do i have to insert what it say?

Thanks so much

Cristian


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>