New to Powershell: elevating a script - powershell-5.1

I have recently been messing around with powershell scripting to simplify my job and have had great success so far, albeit after some struggle. I have created a few scripts to open ADUC, Comp management, and a few other things in an elevated state but I have been having issues with the below script.
Base code:
$Workstation = Read-Host "Workstation\IP Address"
$Username = Read-Host "Username"
$AdminGroup = [ADSI]"WinNT://$Workstation/Administrators,group"
$User = [ADSI]"WinNT://USA/$Username,user"
$AdminGroup.Add($User.Path)
pause
If I right-click and run it as an admin it works, but as soon as I try to add anything to force it to run in an elevated state it seems like it processes, but then when I check the admin group it does not appear to have worked. I could be totally off base here with my code, so any help is appreciated, I am very new to this. I have been scouring google and other forums but haven't found anything that works. Other code I have attempted:
runas /netonly /user:USA\adm$env:USERNAME "$AdminGroup.Add($User.Path)"
The above code would be the preferred method as it would allow the user to elevate it using their admin account that auto populates so they only need to enter the password. When I do this it prompts for the password and has the correct username, the code processes and I get no errors but does not add the user to the admin group.
I have tried this as well, but no success here either.
$arg = "$AdminGroup.Add($User.Path)"
start-process powershell -Verb runas $arg
pause
I have attempted a few other things as well the past few days, but I feel like I am just running in circles at this point. The best I have gotten it is to create a shortcut to the script and set it to run as admin. However, I would like to be able to remove the portion of typing in the username into UAC and just have it request the password in the powershell window when prompted. I did attempt a search on here as well, but haven't had any success. Maybe it's the context I am using while searching for solutions? Any help/advice for a newbie is greatly appreciated!

Related

How in SourceTree to get rid of a window "Git credential manager for Windows"?

After adding the repository, a window with the heading "git credential manager for windows" constantly pops up.
Whatever I do (enter different logins and passwords, cancel), it then appears again. And in more than two copies.
How to get rid of it?
I have registered the following settings in the .hgrc file, but this does not help.
[auth]
repo.prefix = http://to.my.repo
repo.username = <login>
repo.password = <pass>
repo.schemes = http
I placed the question on the site atlassian.com. I hope for a solution) Here is the link.
How-in-SourceTree-to-get-rid-of-a-window-Git-credential-manager on community.atlassian.com
I asked a question, but could not get an answer. I did everything that I was advised, but nothing works. Why does the "Git credential manager for Windows" window access an https server?
https://community.atlassian.com/t5/SourceTree-questions/SourceTree-tries-to-enter-quot-https-quot-although-the-settings/qaq-p/634852#M16629
The problem was solved by the developers of the program. Description of what you need to do can be viewed on the page https://community.atlassian.com/t5/SourceTree-questions/SourceTree-tries-to-enter-quot-https-quot-although-the-settings/qaq-p/634852#U637810
The approach in this blogpost by Ben Gillbanks worked for me.
In short:
Open SourceTree and navigate to the repository you want to update the password of
Click the ‘Terminal’ button to jump to the location of the repo on the command line
Enter ‘Git Pull’ and hit return to update the repository
When requested enter your password
Done
In addition to this approach I first deleted the saved password from Tools>Options>Authentication but I don't know if this made any difference.
1.Goto tools -> options
2.Uncheck "Check default remotes for updates evey [10] minutes"
3.Restart SourceTree!

What contract am I not specifying?

When I try to run my Windows Store app, it won't, and shows me this:
..."does not support the contract specified"? What contract? I'm not implementing any contract; I assume it means the Settings contract or some such, but I'm not doing any of those yet.
I went to the Help page suggested, and it says, in part, "To diagnose these errors
There are no sure ways to fix these errors. Use these techniques to diagnose the problem."
The first suggestion is:
Open Event Viewer (on the Windows Start menu, search for Event Viewer.) In Event Viewer, navigate in the tree to the Application and Services Log\Microsoft\Windows\Apps folder.
Filter the view to event Ids: 5900-6000
Examine the log and see what occurred.
Windows 8.1 only brings up web pages when I search for "Event Viewer"
Actually, I did write a utility to catch event logs, and I ran that. I found these:
***Type: Error
Source: Microsoft-Windows-Immersive-Shell
Time Generated: 11/27/2014 15:10:39
Message: Activation of app App.adaf78a74.a8c80.a4ff5.a99ca.a97636d548196_8wekyb3d8bbwe!Designer.App failed with error: -2144927148 See the Microsoft-Windows-TWinUI/Operational log for additional information.
Type: Error
Source: Microsoft-Windows-Immersive-Shell
Time Generated: 11/27/2014 15:20:08
Message: Activation of app axXAndSpace.Visits_qtyjefqf75sa6!App failed with error: -2144927148 See the Microsoft-Windows-TWinUI/Operational log for additional information.***
But where is "Microsoft-Windows-TWinUI/Operational log"?
The second suggestion on the Help page was:
Use the native debugger
Configure the project to run under a native debugger.
In Visual Studio, set the Debugger Type to Native Only on the Debug (Debugging in C++ and JavaScript) page of the property pages of the start-up project.
Look at the exceptions being thrown by looking at the output window. You might want to configure the debugger to stop when these exceptions are thrown.
I did that, but it gave me no more information than previously - nothing at all displays in the Output pane.
What could be the cause of this?
UPDATE
Just to update the progress:
Based on Iris Classon's suggestions, I went to Control Panel to see if my app was installed so that I could uninstall it, but it's not in that list.
If I search for the app by typing its name, it tries to start up - its splash screen displays, but goes no further than that.
I went to Program Files\WindowsApps, and when selecting it got, "You don't currently have persimmons to access this folder, click continue to continually get access"
That leads me to "You have been denied permission to access this folder; to gain access to this folder, you will need to use the security tab"
Click the "security tab" link takes me to that tab, and a "For special permissions or advanced settings, click Advanced"
I do so.
That takes me to a Permissions tab, which says, "You must have Read permissions to view the properties of this object; Click continue to attempt the operation with administrative permissions"
I then selected (one at a time, as that is all it allows):
Trusted Installer : Full control
System: Read, Write, and Execute
System: Full Control
Administrators: Read & Execute
But so far, none of that seems to do anything good.
UPDATE 2
This problem has just "gone away" today; I don't think it was anything I did. I added a UserControl, tried running the app again, and now it runs - I don't see that error message. However, I do see a seemingly unrelated one. (at least I got further).
I just had this problem on a fresh install of Windows 8.1 on my laptop. I'm not sure which one of these steps solved it, but until somebody can give you a more exact answer here is something to try as I am sure you are keen on coding.
Only Store Apps run from VS gives that error, other Store apps work fine.
I first of all changed permissions on my WindowsApps folder under Program Files and made sure that I had full access to the folder and sub folders. Restarted the laptop, still no luck.
Then I set up an admin guest account (just something I tend to do for when I need to record tutorials- so not related to trying to solve the error). Out of curiosity I tried to create and run the same Windows Store App and got an error that another user had installed the app. Strange, how was it installed if it gave me the error that it couldn't activate the app on the main account?
I logged back in as the main user and uninstalled the application (which was showing up as installed), and I can now run any Store App from VS without problems.
Not sure this is an answer since I'm not sure what made it work, but give it a try and hopefully somebody else can clear this us up. I'll investigate further, but I'm on a train right now with poor WiFi connection :)
Best of luck!
I had the same scenario, here are the steps I did to resolve:
I tried to create packages, chose not to associate with the store.
It said that the certificate is expired, I created a new test certificate.
I created the test packages successfully.
Tried to run the project from VS and it finally ran.
I believe all of this had to do with the certificate. very frustrating and time wasting issue.
I also had the same problem and following steps fixed it. Hope it will help some one else.
Open the appxmanifest file
Go to "Packaging" tab
Select "Choose Certificate" and select "Pick from certificate store" and select your cetificate and click on ok

Spawn xterm and issue commands using Expect

I'm trying to write a GUI using Tcl/tk where the user will select different parameters and hit a 'Submit' button. When the button is pressed, the program will open an xterm window and telnet and perform configurations based on the choices made by the user. I know this should be pretty basic but I've looked everywhere and cannot find a working method. Please advise. Thanks.
I think xterm isn't needed: Expect is able to provide a program (telnet in your case) with a pseudoterminal -- one of the things xterm does for the program it's running. So just google for this combo.
I don't believe that expect can control an X app such as xterm. It can however control a text app like telnet.
You can write a shell script that launches xterm, passing the "-e" option to execute an expect script. That expect script can then launch telnet.

Running an external script from Access

I want my Access application to run an external program (in this case a R script) after the user clicks a button. I use this code:
Dim RetVal
RetVal = Shell("""C:\Program Files\R\R-2.10.1\bin\R.exe"" CMD BATCH --no-environ --silent --no-restore --no-save ""c:\test.R"" ""c:\test-result.txt""", vbHide)
MsgBox RetVal
This works fine, but the VBA code keeps on running while my script is executed. How can I make Access waiting for the script to be finished? Has anybody suggestions about how to give an error message of the script back to Access?
The OpenProcess and WaitForSingleObject combo that #Remou links to, is probably your best bet for doing this. You should take a look at this, it's a nice drop in module for shell and wait.
For returning a message back from the script, you could mess around with redirecting the scripts input and output. This is not for the faint of heart. As an alternative I would redirect the output of the script to a text file, then read in that file after it exits.

how to automate the testing of a text based menu

I have a text based menu running on a remote Linux host. I am using expect to ssh into this host and would like to figure out how to interact with the menus. Interaction involves arrowing up, down and using the enter and back arrow keys. For example,
Disconnect
Data Collection >
Utilities >
Save Changes
When you enter the system Disconnect is highlighted. So simply pressing enter twice you can disconnect from the system. Second enter confirms the disconnect.
The following code will ssh into my system and bring up the menu. If I remove the expect eof and try to send "\r" thinking that this would select the Disconnect menu option I get the following error: "write() failed to write anything - will sleep(1) and retry..."
#!/usr/bin/expect
set env(TERM) vt100
set password abc123
set ipaddr 162.116.11.100
set timeout -1
match_max -d 100000
spawn ssh root#$ipaddr
exp_internal 1
expect "*password:*"
send -- "$password\r"
expect "Last login: *\r"
expect eof
I have looked at the virterm and term_expect examples but cannot figure out how to tweak them to work for me. If someone can point me in the right direction I would greatly appreciate it. What I need to know is can I interact with a text based menu system and what is the correct method for doing this, examples if any exist would be great.
thanks,
-reagan
Try using the autoexpect tool to record an interactive session, and see what the codes look like.
To simplify your life, you might want to setup public key encryption so that you can ssh to the remote host without using a password. Then you can concentrate on testing your software instead of ssh. Google "ssh login without password" for more information. The instructions you will find are straight forward, so don't be afraid.
Have you tried \n\n\r?