質問編集履歴
3
意図的に内容を抹消する行為にあたるため
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Powershellでショートカットのリンク先(参照先)の値を置き換えたい
|
test
CHANGED
@@ -1 +1,87 @@
|
|
1
|
-
|
1
|
+
### 前提・実現したいこと
|
2
|
+
|
3
|
+
デスクトップのショートカットのリンク先(参照先)を変更したいと思っております。
|
4
|
+
|
5
|
+
条件としては、リンク先(参照先)に"\123.456.78.9\"があれば "\123.456.78.8\"に置き換える、変更するというものです。
|
6
|
+
|
7
|
+
エラーの内容からコードに原因等ございますでしょうか。。
|
8
|
+
|
9
|
+
### 発生している問題・エラーメッセージ
|
10
|
+
|
11
|
+
```
|
12
|
+
|
13
|
+
New-Object : 引数 'System.Object[]' を受け入れる位置指定パラメーターが見つかりません。
|
14
|
+
|
15
|
+
発生場所 行:1 文字:12
|
16
|
+
|
17
|
+
+ $wsShell = New-Object -ComObject WScript.Shell $shortcuts | ForEach-O ...
|
18
|
+
|
19
|
+
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
20
|
+
|
21
|
+
+ CategoryInfo : InvalidArgument: (:) [New-Object]、ParameterBindingException
|
22
|
+
|
23
|
+
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
### 該当のソースコード
|
28
|
+
|
29
|
+
```ここに言語名を入力
|
30
|
+
|
31
|
+
#####
|
32
|
+
|
33
|
+
# デスクトップ、およびデスクトップ上のフォルダ内の
|
34
|
+
|
35
|
+
# ショートカットファイルの参照先を書き換える
|
36
|
+
|
37
|
+
#####
|
38
|
+
|
39
|
+
## レガシーな環境用の共有フォルダパス文字列を新しい環境用に置き換えるfuntion
|
40
|
+
|
41
|
+
function ReplacePath([string] $path) {
|
42
|
+
|
43
|
+
# 例1:\192.0.2.1\share1\~ → \file1.example.jp\share1\~
|
44
|
+
|
45
|
+
$path = $path -ireplace "\123.456.78.9\", "\123.456.78.8\"
|
46
|
+
|
47
|
+
Return $path
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
# メイン処理
|
52
|
+
|
53
|
+
Write-Output "デスクトップ上のショートカットの更新を開始します。"
|
54
|
+
|
55
|
+
$desktop = [System.Environment]::GetFolderPath('Desktop')
|
56
|
+
|
57
|
+
$shortcuts = Get-ChildItem $desktop *.lnk -Recurse
|
58
|
+
|
59
|
+
# PowerShellにショートカットを編集できるコマンドレットは無い。WSHを使う。
|
60
|
+
|
61
|
+
$wsShell = New-Object -ComObject WScript.Shell $shortcuts | ForEach-Object {
|
62
|
+
|
63
|
+
# 更新の必要があるショートカットファイルなのかを確認
|
64
|
+
|
65
|
+
$link = $wsShell.CreateShortcut($_.FullName)
|
66
|
+
|
67
|
+
$newTargetPath = ReplacePath($link.TargetPath)
|
68
|
+
|
69
|
+
if ($newTargetPath -eq $link.TargetPath){
|
70
|
+
|
71
|
+
Write-Output ("置き換えの必要なし: " + $_.FullName)
|
72
|
+
|
73
|
+
return
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
# ショートカットの参照先と作業フォルダーを更新
|
78
|
+
|
79
|
+
$link.TargetPath = $newTargetPath
|
80
|
+
|
81
|
+
$link.WorkingDirectory = ReplacePath($link.WorkingDirectory)
|
82
|
+
|
83
|
+
$link.Save();
|
84
|
+
|
85
|
+
Write-Output ("置き換えを実行: " + $_.FullName) } Write-Output "デスクトップ上のショートカットの更新が完了しました。"
|
86
|
+
|
87
|
+
```
|
2
Test
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
TestTestTestTestTest
|
test
CHANGED
@@ -1,101 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
デスクトップのショートカットのリンク先(参照先)を変更したいと思っております。
|
4
|
-
|
5
|
-
条件としては、リンク先(参照先)に"\123.456.78.9\"があれば "\123.456.78.8\"に置き換える、変更するというものです。
|
6
|
-
|
7
|
-
エラーの内容からコードに原因等ございますでしょうか。。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
### 発生している問題・エラーメッセージ
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
```
|
16
|
-
|
17
|
-
New-Object : 引数 'System.Object[]' を受け入れる位置指定パラメーターが見つかりません。
|
18
|
-
|
19
|
-
発生場所 行:1 文字:12
|
20
|
-
|
21
|
-
+ $wsShell = New-Object -ComObject WScript.Shell $shortcuts | ForEach-O ...
|
22
|
-
|
23
|
-
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
24
|
-
|
25
|
-
+ CategoryInfo : InvalidArgument: (:) [New-Object]、ParameterBindingException
|
26
|
-
|
27
|
-
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
|
28
|
-
|
29
|
-
```
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
### 該当のソースコード
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
```ここに言語名を入力
|
38
|
-
|
39
|
-
#####
|
40
|
-
|
41
|
-
# デスクトップ、およびデスクトップ上のフォルダ内の
|
42
|
-
|
43
|
-
# ショートカットファイルの参照先を書き換える
|
44
|
-
|
45
|
-
#####
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
## レガシーな環境用の共有フォルダパス文字列を新しい環境用に置き換えるfuntion
|
50
|
-
|
51
|
-
function ReplacePath([string] $path) {
|
52
|
-
|
53
|
-
# 例1:\192.0.2.1\share1\~ → \file1.example.jp\share1\~
|
54
|
-
|
55
|
-
$path = $path -ireplace "\123.456.78.9\", "\123.456.78.8\"
|
56
|
-
|
57
|
-
Return $path
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
# メイン処理
|
64
|
-
|
65
|
-
Write-Output "デスクトップ上のショートカットの更新を開始します。"
|
66
|
-
|
67
|
-
$desktop = [System.Environment]::GetFolderPath('Desktop')
|
68
|
-
|
69
|
-
$shortcuts = Get-ChildItem $desktop *.lnk -Recurse
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
# PowerShellにショートカットを編集できるコマンドレットは無い。WSHを使う。
|
74
|
-
|
75
|
-
$wsShell = New-Object -ComObject WScript.Shell $shortcuts | ForEach-Object {
|
76
|
-
|
77
|
-
# 更新の必要があるショートカットファイルなのかを確認
|
78
|
-
|
79
|
-
$link = $wsShell.CreateShortcut($_.FullName)
|
80
|
-
|
81
|
-
$newTargetPath = ReplacePath($link.TargetPath)
|
82
|
-
|
83
|
-
if ($newTargetPath -eq $link.TargetPath){
|
84
|
-
|
85
|
-
Write-Output ("置き換えの必要なし: " + $_.FullName)
|
86
|
-
|
87
|
-
return
|
88
|
-
|
89
|
-
}
|
90
|
-
|
91
|
-
# ショートカットの参照先と作業フォルダーを更新
|
92
|
-
|
93
|
-
$link.TargetPath = $newTargetPath
|
94
|
-
|
95
|
-
$link.WorkingDirectory = ReplacePath($link.WorkingDirectory)
|
96
|
-
|
97
|
-
$link.Save();
|
98
|
-
|
99
|
-
Write-Output ("置き換えを実行: " + $_.FullName) } Write-Output "デスクトップ上のショートカットの更新が完了しました。"
|
100
|
-
|
101
|
-
```
|
1
|
+
TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest
|
1
誤字変更
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|