Singol Thread Error (STA) when open script by powershell

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
mpleteSource value)
System.Windows.Forms.DataGridViewComboBoxCell.InitializeEditingCo
ntrol (1nt32 rowlndex,, Object
initialFormattedValue,,
DataGridViewCellStyle dataGridViewCellStyle)
System.Windows.Forms.DataGridViewJnitializeEditingC ontrolValue
(Dat
aGridViewCellStyle & dataGridViewCellStyle,,
DataGridViewCell
DataGridViewCell)
To replace this default dialog box,
handle the event
DataError.
How can i fix this? where do i have to insert what it say?
Thanks so much
Cristian

I forget which versions this applies to, but some of the older versions of PowerShell launch the console in MTA mode by default, while the ISE is always STA. On my computer with PowerShell 4.0 installed, this is no longer the case; both ISE and Console both
use STA by default.
Anyhow, just use the -Sta switch when launching PowerShell.exe, and you'll be able to run that script. You could even add code to the beginning of the script which detects the current apartment state, and relaunches a new copy of powershell.exe if needed,
very similar to code which people have used to detect whether PowerShell.exe was running in an elevated process. Something along these lines:
if ([System.Threading.Thread]::CurrentThread.ApartmentState -eq [System.Threading.ApartmentState]::MTA)
powershell.exe -Sta -File $MyInvocation.MyCommand.Path
return
# Rest of your script code here

Similar Messages

  • I had to reinstall CS4 and now have error messages when opening bridge and photoshop

    The error message is "The specified module could not be found. C:\Program Files (x86)\Common Files\Adobe\Adobe Version Cue CS4\Client\4.0.1\Version Cue.DLL

    I tried what you suggested and it did not help.  whenever I try to open either photoshop or bridge, I get a message saying that there was a problem loading scripts the last time that Bridge was open and do I want to try loading them again?  If I click yes, I get that same error message that I reported in my original question and photoshop/bridge will immediately close.  If I click no, photoshop and bridge will open but it will not let me open a raw format document.  It seems like Adobe Camera Raw is not being recognized.   One of the updates I downloaded was an update to Adobe Camera Raw so the functionality is there, it just can't be accessed.  do you think I should try to uninstall and re-install Photoshop again?
    Date: Mon, 3 Sep 2012 19:48:31 -0600
    From: [email protected]
    To: [email protected]
    Subject: I had to reinstall CS4 and now have error messages when opening bridge and photoshop
        Re: I had to reinstall CS4 and now have error messages when opening bridge and photoshop
        created by Arpit Kapoor in Downloading, Installing, Setting Up - View the full discussion
    Go To Help->Updates and install the latest update. It should work.
         Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/4670793#4670793
         To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/4670793#4670793. In the Actions box on the right, click the Stop Email Notifications link.
         Start a new discussion in Downloading, Installing, Setting Up by email or at Adobe Forums
      For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • Error message when opening CS6 files in earlier versions

    PSCS6 with 12.0.4 update.  Mac OSX 10.8.2   16GB 1333 Mhz DDR3  543 GB Available
    I've recently become aware that other people are getting an error message when opening many of my my psd files if they are on earlier versions of PS and also on PSE, (including the latest version).  The message is:
    This document contains unknown data which will be discarded to keep layers editable.  To preserve the original appearance instead, choose flatten to load composite data as a flattened image. 
    Three options follow:  Flatten  Cancel  Keep Layers
    When 'keep layers' is selected the file opens as normal.  However, people with much earlier version (CS version 8) don't get the option to keep layers, and the layers are flattened when the document is opened.
    I still have CS5 on my mac, and I find that if I take my finished file into CS5 and save it there, the customer does not get the message above, and the file behaves as it should.  Those on very early version of PS can also open the files without any issues. 
    I've been using CS6 since it was released and have had to delve through all my files and save them in CS5.  Not every file is affected and I've been unable to find a common thread.
    Hoping someone can throw some light on this.

    This similar to the message one should get in an older version of photoshop such as photoshop 7.
    If one presses ok instead of read composite that might preserve the layers with the original look.
    But sometimes if a layer blending mode or some other feature is used an earlier versions of photoshop doesn't have, reading the composite is the only way to preserve the look of the image.
    You said your using shape layers?
    If they are shape layers you didn't use a stroke from the tool options bar?
    Can you post an example of the image with the layers panel visible that you get that message with?
    I guess your using photoshop cs6 with the 13.01 update?

  • Error message when opening Photoshop CC

    For the last month, I've been getting an error message when opening Photoshop CC that says:
    "Could not load actions because an unexpected end-of-file was encountered"
    I've been having to go back and use CS5 in order to get anything accomplished between this and my plugins not showing up (post about plugins in another thread).

    See my reply #1 in your other thread http://forums.adobe.com/thread/1419961?tstart=0

  • Fatal Error Message when opening PhotoshopCS4

    I am receiving a fatal error message when opening photoshop cs4. "Fatal Error -Missing Component (/Library/Application Support/Adobe/Adobe Version Cue CS4/Client/4.0.0/VersionCue.framework" I was recently using the trial version of Photoshop CS5.1 but that trial period has ran out. Now I cannot open my file in CS4. Within that trial period, I also switched from using Snow Leopard to Lion OS. Could anyone shed some light on the issue?! I need to get some work done!

    See my reply #1 in your other thread http://forums.adobe.com/thread/1419961?tstart=0

  • I have an Error 16 when opening Dreamweaver CC

    I have an Error 16 when opening Dreamweaver CC - what should I do?

    Error 16 http://helpx.adobe.com/x-productkb/policy-pricing/configuration-error-cs5.html
    -including DW039 https://forums.adobe.com/thread/1500609

  • I am getting a 42404 error message when opening itunes and itunes will not let me connect my iphone to it trying to get me to restore. I need help?

    I am getting a 42404 error message when opening itunes and itunes will not let me connect my iphone to it trying to get me to restore. I need help? I am not that technically inclined and really simple terms  are appreciated.

    Try downloading the songs from a different device. If you are doing it from your iOS device, try form your computer.

  • Error Message when open iTunes -42032. What can I do?

    Error Message when open iTunes -42032. What can I do?

    Try 2 solutions: 1.  Press both Ctrl and Shift keys then double click on ITunes.  Hold keys down until a box appears on screen indicating ITunes is starting in "safe mode'.  left click on 'continue" and ITunes will run without closing.
    2;  Start ITunes normally, then  after loading, choose "Match" and then touch "stop" beforre step 3 , Uploading songs...",  runs for more than a few seconds.  The code problems are in Match;  the uploading function in Match.  ITunes won't crash if you stop Match soon enough!

  • Error message when opening a document "this document is being used by another user

    error message when opening a document "this document is being used by another user, do you want to make a copy?
    I select yes, then new message - word can not open this document.the document might be in use or it may not be an invalid document.
    Any suggestions?

    Hello MrNeuf,
    I would recommend releasing and renewing the IP lease on the affected computer. The following article provides further information and details the process of doing so.
    Mac OS: How to release and renew a DHCP lease
    http://support.apple.com/kb/TS1920
    Cheers,
    Allen

  • Error message when opening adobe premier 10 saved film file , this file is not supported or required codec not installed

    Hi
    Can any one help, keep getting error message,error message when opening adobe premier 10 film file , this file is not supported or required codec not installed,
    Thanks
    Colin65

    Steve,
    Not to sure what type AVI files they are, the storage devise is at present offshore on a job.
    Can you clarify, if you edit/import video footage with adobe premiere 10 and then create a file, the file or the original raw footage is not stored on computer for re-use, if it is stored, is it stored in the original format.
    Also please find details of storage devise,
    Dogcam Portable Media Center PMC-500r
    Quick DetailsModel Number:PMC-500R
    SpecificationsPMC-500R20G/40G Portable Multimedia PlayerPMC-500R 20G/40G Portable Multimedia Player, Supports
    PMC-500R20G/40G Portable Multimedia PlayerPMC-500R 20G/40G Portable Multimedia Player, Supports MPEG-4 File FormatsKey Specifications/Special Features:Video recorder: supports digital video recoding in MPEG4 from Line-in Video playback: supports MPEG4 (AVI), MPEG1 (.MPG), DVD (MPEG2,.VOB) Supports MPEG4 with 720 x 480 at 30 Frames/s MPEG4 movie playing with Fast Forward, Fast Reverse, Time search function Music playback: supports MP3, WMA, WAV Music playing with play list function MP3 ID3 information and sorting, LRC Music playing with repeat, shuffle playing function Photos view: supports JPEG and slide show, rotate, nail view Easy backup of photos in digital camera -USB OTG technology Audio recording: continues recording for over 40 hours High resolution output for HDTV (Y, U, V output; 704 x 480 (N) or 704 x 576 (P), at 25/30 frames/s) Super dlim size: 126 x 79 x 22.5 mm Games E-book with bookmark, auto turning page, page search function File browser function with Copy, Delete, HDD capacity information With 3.5" LCD QVGA TFT panel (960 x 240) Long time battery running (over 5 hours for video, and 9 hours for audio) 1.8" 20G/40G storage capacity High fidelity sound with good earphoneCompass Electronics Co.,

  • HT2041 I receive an error message when opening up a downloaded program as "Safari can't open the file because no availabel application can open it". Not sure what exactly this means or what I have to do!

    I receive an error message when opening up a downloaded program "safari can't open the file because no available application can open it" I am not sure what I need to do to get these programs open!

    What is the file?
    Where did you get it from?
    Allan

  • "The exception unknown software exception (0xc06d007e) occured in the application at location 0x7c812aeb" - error displayed when opening PDF files. What i do?

    "The exception unknown software exception (0xc06d007e) occured in the application at location 0x7c812aeb" - error displayed when opening PDF files. What i do?

    What is your Reader XI version?
    Have you tried to disable Protected Mode [Edit | Preferences | Security (Enhanced)] ?

  • TS1424 error (-42110) when opening itunes, error (3014) when trying to restore ipod. my ipod is now frozen and the screen says connect to itunes. help!

    i get error (-42110) when opening itunes, error (3014) when trying to restore ipod. my ipod is now frozen and the screen says connect to itunes. help!

    Did you forget the passcode?
    http://support.apple.com/kb/ht1212
    http://support.apple.com/kb/ht1808

  • Priemier Elements 12 pops up a error screen when opening project." No disc in drive E.Please insert

    Priemier Elements 12 pops up a error screen when opening project." No disc in drive E.Please insert disc to continue." My DVD drives are  F and D. What do I have to do to get rid of this error?

    charkmapman
    What computer operating system is your Premiere Elements 12 running on?
    In versions of Premiere Elements earlier than 12, the answer for that type of "No disc" message was usually related to SmartSound/Sonicfire Pro 5 feature. But, there is no SmartSound/SonicfirePro 5 feature in version 12, only Music Scores without the SmartSound name.
    Consequently, I would suggest an uninstall, do a ccleaner run through (regular cleaner and registry cleaner parts), reinstall.
    http://www.piriform.com/ccleaner
    We will be watching for the results.
    Thanks.
    ATR

  • I always got error message when opening BlackBerry App World using 3g

    Hi there, I always got error message when opening BlackBerry App World using 3g network, but it's fine if I open it through wifi network. Does anyone ever had the same problem and how to fix it? Thanks.

    You need the latest version of BlackBerry World installed on your smartp

Maybe you are looking for

  • Roto Brush issue in CS5 Premiere Pro

    I am a new CS5 user with a week of learning under my belt.  (My previous experience was splicing 8mm film together when making films as a kid.)  I am hoping someone can advise on an issue I am encountering in CS5 Premiere Pro?  (I realize Roto Brush

  • Number of Placed Photos Missing in Book Editor

    Recently my copy of Aperture stopped displaying the number badge that indicates the number of times an image has been placed in a book. The example of the indicator is shown on p. 372 of the Aperture User Manual. I have searched the Book preferences

  • How to make validation in Bean and select value from another table

    I want to know how to select data from table in backing bean according to primary key i have the problem is that i have a table Employee_Salary contains Employee ids and their salary Empoloyee_Salary table      Employee_ID      Number      Employee_s

  • Lost photos in iphoto library...help

    I have no idea what happened but when I open my iphoto application, all of my photos/albums/projects have disappeared. When I look at ALL Documents, my photos are there. Any ideas about how to get them back into my iphoto library? There is some iPhot

  • Urgent, my HDD is dying! : Will a Lenovo MT-M 5485 -AB6 take only a SATA III HDD?

    My current HDD is a WD5000AAKS and I've been told it is not long for this world. I need to replace it, but do I need to buy the exact same model? The HDD has 500GB capacity, what is the largest capacity storage HDD the machine can handle if the repla