Run windows system server in golang - windows

I want to use netsh in my app ,the netsh depend some windows server,so i write the bat file ,before i run app i will start these system server
bat file
regsvr32.exe /s C:\Windows\system32\Dhcp.dll
regsvr32.exe /s C:\Windows\system32\RpcSs.dll
regsvr32.exe /s C:\Windows\system32\nsi.dll
regsvr32.exe /s C:\Windows\system32\NetTcpPortSharing.dll
regsvr32.exe /s C:\Windows\system32\w32time.dll
regsvr32.exe /s C:\Windows\system32\iphlpsvc.dll
sc config Dhcp start= auto
net start Dhcp
sc config RpcSs start= auto
net start RpcSs
sc config nsi start= auto
net start nsi
sc config NetTcpPortSharing start= auto
net start NetTcpPortSharing
sc config w32time start= auto
net start w32time
sc config iphlpsvc start= auto
net start iphlpsvc
netsh interface portproxy reset
golang code
func StartIpHelper() {
cmd := exec.Command("cmd", "/c", "start_depend.bat")
if err := cmd.Start(); err != nil {
logger.Errorln("start depend windows server err:", err)
return
}
}
My question
1. in my bat file ,some system server can not run. Maybe my exe do not have Administrator authority
I try this resp ,but not work as win7 and others windows sersiongith
https://github.com/mozey/run-as-admin
2.maybe i run exec.cmd() ,apply Administrator authority

Related

Message Description Popup while sending message to same network in CMD

I want to send message from Dell-PC to Acer-PC
Both are connected to same wifi network.
Dell-PC - Windows 10
Acer-PC - Windows 7
But when ever i use msg command, i get the following:-
The command i use:-
msg /SERVER:Acer-PC * /TIME:20 "Welcome!"
This answer is an hybrid of Batch and VBScript
You will need to enable LocalAccountTokenFilterPolicy value in remote computer:
In that computer open CMD as administrator and type:
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
It should return The operation completed successfully.
Next, create the message box file. For that use a tool called PsExec from Windows Sysinternals
When you have downloaded PsExec, run the following command:
PsExec \\Acer-PC -e CMD /c echo CreateObject("WScript.Shell").Popup "Welcome!",20,"" ^> C:\MessageBox.vbs
To run the message box, type the following from your computer (Acer-PC):
PsExec \\Acer-PC -e WScript.exe C:\MessageBox.vbs
It will appair the message during 20 seconds

Packer cannot communicate with AWS windows server 2016 instances to build custom ami

I am using packer 0.12.1 to build an AWS ami based on the existing windows server 2016 ami.
The way I used to get it to work with windows server 2012 is to specify in the packer template:
"communicator": "winrm",
"winrm_username": "Administrator",
"winrm_use_ssl": true,
"winrm_insecure": true,
"user_data_file":"./ec2-userdata-winserver2016.ps1",
And the user data file would look like:
<powershell>
write-output "Running User Data Script"
write-host "(host) Running User Data Script"
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore
# Don't set this before Set-ExecutionPolicy as it throws an error
$ErrorActionPreference = "stop"
# Remove HTTP listener
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
$Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName "packer"
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint -Force
# WinRM
write-output "Setting up WinRM"
write-host "(host) setting up WinRM"
cmd.exe /c winrm quickconfig -q
cmd.exe /c winrm set "winrm/config" '#{MaxTimeoutms="1800000"}'
cmd.exe /c winrm set "winrm/config/winrs" '#{MaxMemoryPerShellMB="1024"}'
cmd.exe /c winrm set "winrm/config/service" '#{AllowUnencrypted="true"}'
cmd.exe /c winrm set "winrm/config/client" '#{AllowUnencrypted="true"}'
cmd.exe /c winrm set "winrm/config/service/auth" '#{Basic="true"}'
cmd.exe /c winrm set "winrm/config/client/auth" '#{Basic="true"}'
cmd.exe /c winrm set "winrm/config/service/auth" '#{CredSSP="true"}'
cmd.exe /c winrm set "winrm/config/listener?Address=*+Transport=HTTPS" "#{Port=`"5986`";Hostname=`"packer`";CertificateThumbprint=`"$($Cert.Thumbprint)`"}"
cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
cmd.exe /c netsh firewall add portopening TCP 5986 "Port 5986"
cmd.exe /c net stop winrm
cmd.exe /c sc config winrm start= auto
cmd.exe /c net start winrm
</powershell>
This works fine in windows server 2012, but not anymore in windows server 2016.
For server 2016, the output of packer is:
==> eu-west-1-builder: Waiting for auto-generated password for instance...
eu-west-1-builder: It is normal for this process to take up to 15 minutes,
eu-west-1-builder: but it usually takes around 5. Please wait.
eu-west-1-builder:
eu-west-1-builder: Password retrieved!
==> eu-west-1-builder: Waiting for WinRM to become available...
And it hangs until timeout.
As AWS release windows server 2016 ami fairly recently, I couldn't find much information. I am not really a windows guru (I am usually on linux)
Any help / suggestions would be greatly appreciated.
Try changing "winrm_username" to something other than Administrator
As per Packer documentationThe default user name is packer not root as in other builders. Most distros on Azure do not allow root to SSH to a VM hence the need for a non-root default user. Set the ssh_username option to override the default value.

Running the .bat batch file in administrator mode

Iam trying to synch certain computers to a TimeServer within the network, hence i've written a simple batch script to do the task. I've put into our WDS server.
How do i run it in administrator mode.
If i open the cmd prompt as administrator it starts with "C:\Windows\System32\"
this is where i want to run so how do i achieve it.I dont want to schedule it nor i want to use RMB Click and select "Run as Administrator"
OS: Win 2012R2
My batch file contains.
reg add HKLM\SYSTEM\CurrentControlSet\services\w32time\Config /v MaxNegPhaseCorrection /t REG_DWORD /d 0xffffffff /f
reg add HKLM\SYSTEM\CurrentControlSet\services\w32time\Config /v MaxPosPhaseCorrection /t REG_DWORD /d 0xffffffff /f
w32tm /config /syncfromflags:manual /manualpeerlist:"132.186.XX.XX 132.186.127.XX 132.186.192.XX 132.186.XXX.XX" /largephaseoffset:120000
net stop w32time
net start w32time
w32tm /resync
Thanks in Advance
Create a shortcut to the batch file.
Right click on the shortcut, go to properties -> shortcut tab -> advanced.
Tick run as adminstrator
Add this to the start of your batch file:
runas.exe /savecred /user:#administratoraccount# "%windir%/System32/cmd.exe"
Replace #administratoraccount# with the username of an admin.
The first time you run it, it will ask for the password of that admin account. Enter it. Unless the password is removed from the windows credential locker or the password or username of that admin is changed or the username is disabled, whenever the batch file is run from then on, it will automatically run cmd.exe as administrator.

How to run multiple commands in one batch file?

I want to run multiple commands in one batch file.
I tried the &, &&, start, /wait, call, :begin and goto begin commands but no luck.
Here are my commands:
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
sc config remoteregistry start= auto
sc start remoteregistry
sc config Schedule start=auto
sc start Schedule
sc stop McAfeeFramework
sc configure McAfeeFramework startup= disabled
sc stop McShield
sc configure McShield startup= disabled
sc stop McTaskManager
sc configure McTaskManager startup= disabled
netsh advfirewall set AllProfiles state off
sc stop MpsSvc
sc config MpsSvc start= disabled
C:\Windows\System32\cmd.exe /k <command>
starts a new cmd context where is executed, but /k keeps that new context open. You want to close it after executing , so further commands from the original context can be executed. Use /c instead of /k to do so.
described in cmd /?
Well, batch scripts already do it by default, but i guess that your usage of /K on cmd.exe it was unnecessary and harmful, and A / C could have done the job even though, remembering, it is not necessary.
File1.bat*
C:\Windows\System32\cmd.exe /c %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
sc config remoteregistry start= auto
sc start remoteregistry
sc config Schedule start=auto
sc start Schedule
sc stop McAfeeFramework
sc configure McAfeeFramework startup= disabled
sc stop McShield
sc configure McShield startup= disabled
sc stop McTaskManager
sc configure McTaskManager startup= disabled
netsh advfirewall set AllProfiles state off
sc stop MpsSvc
sc config MpsSvc start= disabled

RDP on Windows10 for RaspberryPi

How can I activate Remote Desktop Protocol on Windows10 through commandline?
I have the SSH running from Mac to Win10, but are not get RDP working.
I tried this CMD command for activation:
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
Rebooted with:
shutdown /s /t 0
And looked for listening ports with:
netstat /p tcp /a
Windows 10 IoT Core doesn't support RDP up to now.
The only "remote" ways are :
Powershell
SSH
Web Interface
Paolo.

Resources