-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowershell.sh
executable file
·80 lines (58 loc) · 2.1 KB
/
powershell.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Executes Powershell via OpenSSH for Windows from Linux
# 16.9.2016 Bernd Broermann
# 19.9.2016 Bernd Broermann
# export variables POWERSHELLHOST, POWERSHELLUSER and POWERSHELLDOMAIN in your .bashrc
# otherwise they will be set to
POWERSHELLDOMAIN=${POWERSHELLDOMAIN=EXAMPLE}
POWERSHELLHOST=${POWERSHELLHOST-win2012withopenssh.example.com}
POWERSHELLUSER=${POWERSHELLUSER-$USER}
DEBUG=${DEBUG-false}
VERBOSE=false
usage () {
echo "Usage:
`basename $0` # invokes an ugly Powershell
`basename $0` -c 'Command Arg1 Arg2' # executes \"-Command\" -eg. 'Get-Module -ListAvailable'
Command must be quotet!!!
`basename $0` -f Commandfile # executes ps1 file
-H winhost # overwrites exported POWERSHELLHOST
-u winusername # overwrites exported POWERSHELLDOMAIN\\\\\POWERSHELLUSER
Backslash must be doubled!!!
-v be verbose"
exit 1
}
while getopts 'c:f:u:H:hv' OPTION ; do
case $OPTION in
u) POWERSHELLUSER=$OPTARG;;
H) POWERSHELLHOST=$OPTARG;;
c) COMMAND=$OPTARG;;
f) COMMANDFILE=$OPTARG ;;
v) VERBOSE=true ;;
h) usage;;
esac
done
if test -z $COMMANDFILE
then
COMMANDFILE="powershell.ps1"
if test ! -z "$COMMAND"
then echo -e "$COMMAND\n" > $COMMANDFILE
else COMMANDFILE="-"
fi
fi
if $VERBOSE ; then
echo "
POWERSHELLUSER= [$POWERSHELLUSER]
POWERSHELLHOST= [$POWERSHELLHOST]
POWERSHELLDOMAIN= [$POWERSHELLDOMAIN]
COMMAND= [$COMMAND]
COMMANDFILE= [$COMMANDFILE]
"
set -x
fi
# copy COMMANDFILE to windows
if test $COMMANDFILE != "-" ; then
echo "put $COMMANDFILE" | sftp -b - $POWERSHELLDOMAIN\\$POWERSHELLUSER@$POWERSHELLHOST &>/dev/null
# rm $COMMANDFILE
fi
# execute COMMANDFILE on windows
ssh -a -x $POWERSHELLDOMAIN\\$POWERSHELLUSER@$POWERSHELLHOST Powershell -File $COMMANDFILE