Search code examples
c#asp.net.netpowershellpowershell-cmdlet

-ConnectionUri parameter error with Remote Session trying to pass message to computer on network


I am currently trying to make an IIS based application with asp.net and C# to serve as a notification system that someone is here to see someone at our front desk so we don't have to go chase them down. I keep getting an error that reads "One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings" in the debugger for visual studio.

I have tried using credentials thinking it was a permission to remote issue, I have tried making the it a var as seen below. I tried a string.format also thinking it may not like the type.

Variable for ComputerName is just the basic computer name the computer is given not the full computername going to be trying that next.

var ComputerName = c.Attribute("CN");

InitialSessionState initial = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand("invoke-command"); 
ps.AddParameter("ComputerName", ComputerName);
ScriptBlock calldown = ScriptBlock.Create("Get-childitem C:\\windows");
ps.AddParameter("ScriptBlock", calldown);

foreach (PSObject obj in ps.Invoke()){
string message = string.Format("You currently have a client waiting for you at the front desk, please check the lobby system to find out more");
string title = string.Format ("Lobby Alert");

MessageBox.Show(message, title);
}

The results should be a message box on a computer in the back that is not the user "frontdesk" computer.


Solution

  • Solved it by using the value object instead since it registers as type string to pass through this error now have a different issue but that isn't Powershell related so I am posting a more accurate question in the C# section for that error I am having.

    var ComputerName = c.Attribute("CN");
    
    InitialSessionState initial = InitialSessionState.CreateDefault();
    Runspace runspace = RunspaceFactory.CreateRunspace(initial);
    runspace.Open();
    PowerShell ps = PowerShell.Create();
    ps.Runspace = runspace;
    ps.AddCommand("invoke-command"); 
    ps.AddParameter("ComputerName", ComputerName.Value);
    ScriptBlock calldown = ScriptBlock.Create("Get-childitem C:\\windows");
    ps.AddParameter("ScriptBlock", calldown);
    
    foreach (PSObject obj in ps.Invoke()){
    string message = string.Format("You currently have a client waiting for you at the 
    front desk, please check the lobby system to find out more");
    string title = string.Format ("Lobby Alert");
    
    MessageBox.Show(message, title);}