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