Os.system called powershell with an exception

HI:
When I called powershell using os.system, I reported an error:
Add Type: c: .…\Ufurftp.0.cs (1): Invalid token ‘,’ in class, structure, or interface member declaration.
Add-Type : c:.…\uf1urftp.0.cs(1) : Expected input should be class, delegate, enum, interface, or struct
Add-Type : c:.…\uf1urftp.0.cs(1) : Expected type, namespace definition, or end of file input
located in
Add-Type 'using System;using System.Text;using System.Runtime.Interop
This command works normally when running with powershell.
Of course, the usual approach is to use APIs. But our laboratory computers cannot be connected to the internet and cannot use USB drives, so we cannot install third-party APIs. That’s why I used this method.
#—————————————— update——————————————————

Several issues have been identified now

  1. Double quotation marks "will be lost when transitioning from Os. system to powershell.
    Resolved through dll=r “” user32. dll “”.
  2. The string “=>{” will change to “=” when going to powershell
    This symbol “>” will output the error report to a file named “{”. This “{” is the character after “=>{”.
    It may be a functional conflict. But it cannot be resolved. No matter what format “>” is set, the final result will be the same.
    I don’t know how to solve this yet. I have tried many methods. All transcribed as’=’

#—————————————— My error code:——————————————————

    win_name="notepad"
    dll=r"\"user32.dll\""
    a="Add-Type 'using System;using System.Text;using System.Runtime.InteropServices;public class WinApi\
    {public delegate bool WndEnumProc(IntPtr hWnd, int lParam);\
        [DllImport("+dll+")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);\
        [DllImport("+dll+")]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);\
        [DllImport("+dll+")]public static extern bool IsWindowVisible(IntPtr hWnd);\
        [DllImport("+dll+")]public static extern bool EnumWindows(WndEnumProc lpEnumFunc, int lParam);\
        [DllImport("+dll+")]public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);\
        [DllImport("+dll+")]public static extern int SendMessage(int hwnd, int wMsg, int wParam, Byte[] lParam);\
        public static IntPtr FindWindowExByDimStrIntoWindow(string dimStr)\
        {IntPtr iResult = IntPtr.Zero;\
            string controlTitle = dimStr;\
            bool i = EnumWindows(\
            (IntPtr h,int l) =>\
            { int cTxtLen; \
                if (IsWindowVisible(h))\
                {cTxtLen = SendMessage(h, 0xe, 0, 0);\
                Byte[] byt = new Byte[cTxtLen]; SendMessage((int)h, 0xd, cTxtLen + 1, byt);\
                string str = Encoding.Default.GetString(byt);\
                    if (str.ToString().Contains(dimStr))\
                    {iResult = h;\
                        controlTitle = str.ToString();\
                        return false; \
                    }else \
                        return true;\
                }else \
                    return true;\
            }, 0);\
            return iResult;\
        }\
        public static void Show(string dimStr)\
        {IntPtr mainHandle = FindWindowExByDimStrIntoWindow(dimStr);\
            if (mainHandle != IntPtr.Zero)\
            {ShowWindow(mainHandle, 6);\
            ShowWindow(mainHandle, 9);\
            }\
        }\
    }\
';\
$win='e5052';[WinApi]::Show($win)"
    b="powershell -c "+a
    os.system(b)

#———————————————Error message—————————————

Add-Type : c:\Users\win\AppData\Local\Temp\dsin0tqa.0.cs(1) : Invalid token ',' in class, structure, or interface member declaration
c:\Users\win\AppData\Local\Temp\dsin0tqa.0.cs(1) : >>> using System;using System.Text;using System.Runtime.InteropServices;publ
ic class WinApi {public delegate bool WndEnumProc(IntPtr hWnd, int lParam); [DllImport("user32.dll")]public static extern IntPt
r FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")]public static extern bool ShowWindow(IntPtr hWn
d, int nCmdShow); [DllImport("user32.dll")]public static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")]pub
lic static extern bool EnumWindows(WndEnumProc lpEnumFunc, int lParam); [DllImport("user32.dll")]public static extern int SendM
essage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImport("user32.dll")]public static extern int SendMessage(int hwnd, i
nt wMsg, int wParam, Byte[] lParam); public static IntPtr FindWindowExByDimStrIntoWindow(string dimStr) {IntPtr iResult = IntPt
r.Zero; string controlTitle = dimStr; bool i = EnumWindows( (IntPtr h,int l) = int cTxtLen; if (IsWindowVisible(h)) {cTxtLen = 
SendMessage(h, 0xe, 0, 0); Byte[] byt = new Byte[cTxtLen]; SendMessage((int)h, 0xd, cTxtLen + 1, byt); string str = Encoding.De
fault.GetString(byt); if (str.ToString().Contains(dimStr)) {iResult = h; controlTitle = str.ToString(); return false; }else ret
urn true; }else return true; }, 0); return iResult; } public static void Show(string dimStr) {IntPtr mainHandle = FindWindowExB
yDimStrIntoWindow(dimStr); if (mainHandle != IntPtr.Zero) {ShowWindow(mainHandle, 6); ShowWindow(mainHandle, 9); } } }
Position Line: 1 Character: 1

+ Add-Type 'using System;using System.Text;using System.Runtime.Interop ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type],Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

#————————————————————————————————
I have written similar usage before. Used to take screenshots of the computer.
So I can rule out issues with my software environment.

#—————————— My another OK code:——————————————————————

def pc_screenshot(fname="D:\\test.jpeg",width="1920",height="1080",top="0",left="0",x="0",y="0"):
    width=str(width)
    height=str(height)
    top=str(top)
    left=str(left)
    x=str(x)
    y=str(y)
    fname=str(fname)
    fname=fname.replace('\"','')
    fname=fname.replace('\'','')
    a="$Path='"+fname+"';\
        Add-Type -AssemblyName System.Windows.Forms;\
        $screen = [System.Windows.Forms.SystemInformation]::VirtualScreen;\
        $width = "+width+";\
        $height = "+height+";\
        $left = "+left+";\
        $top = "+top+";\
        $bitmap = [System.Drawing.Bitmap]::new($width, $height);\
        $MyDrawing = [System.Drawing.Graphics]::FromImage($bitmap);\
        $MyDrawing.CopyFromScreen($left, $top, "+x+", "+y+", $bitmap.Size);\
        $bitmap.Save($Path)"
    b="powershell -c "+a
    os.system(b)

#————————————————————————————————

Thank you. I’m sorry for my poor English.

It might be that not all the quotes are escaped properly.

For example, there’s this part:

string controlTitle = "";\

Shouldn’t that be:

string controlTitle = \"\";\

?

Also, when posting copy or traceback, select the code or traceback that you posted and then click the </> button.

This is the icon for a button in the discuss UI not mark up you type.
Use 3 back quotes

```
type or paste code here
```

Thank you for your reply

It may be related to this issue. I changed this but still reported an error.
I changed the initial value of the string.

string controlTitle = dimStr;\

The internal string is represented in other ways

dll=r"\"user32.dll\""

Still reporting the same question

After replacing (Os.system) with (subprocess.Popen), the problem was resolved.
But the reason for it is not yet known.
It may be related to the>symbol, which is a redirection function in the command.